11using System ;
22using System . Collections . Generic ;
3+ using System . Collections . ObjectModel ;
34using System . ComponentModel ;
45using System . Reflection ;
56using System . Text ;
@@ -67,47 +68,46 @@ public virtual QpClient CreateClient()
6768 throw new NotImplementedException ( ) ;
6869 }
6970
71+ protected virtual void LoadFromQueryString ( string key , string value )
72+ {
73+ switch ( key )
74+ {
75+ case nameof ( ConnectionTimeout ) :
76+ ConnectionTimeout = int . Parse ( value ) ;
77+ break ;
78+ case nameof ( EnableCompress ) :
79+ EnableCompress = bool . Parse ( value ) ;
80+ break ;
81+ case nameof ( EnableEncrypt ) :
82+ EnableEncrypt = bool . Parse ( value ) ;
83+ break ;
84+ case nameof ( EnableNetstat ) :
85+ EnableNetstat = bool . Parse ( value ) ;
86+ break ;
87+ case nameof ( MaxPackageSize ) :
88+ MaxPackageSize = int . Parse ( value ) ;
89+ break ;
90+ case nameof ( Password ) :
91+ Password = value ;
92+ break ;
93+ case nameof ( RaiseNoticePackageReceivedEvent ) :
94+ RaiseNoticePackageReceivedEvent = bool . Parse ( value ) ;
95+ break ;
96+ case nameof ( TransportTimeout ) :
97+ TransportTimeout = int . Parse ( value ) ;
98+ break ;
99+ }
100+ }
101+
70102 protected virtual void LoadFromUri ( Uri uri )
71103 {
72104 if ( string . IsNullOrEmpty ( uri . Query ) )
73105 return ;
74106 var queryString = System . Web . HttpUtility . ParseQueryString ( uri . Query ) ;
75- var type = this . GetType ( ) ;
76107 foreach ( var key in queryString . AllKeys )
77108 {
78- var pi = type . GetProperty ( key ) ;
79- if ( pi == null )
80- continue ;
81- var propertyType = pi . PropertyType ;
82- var stringValue = queryString [ key ] ;
83- object propertyValue = stringValue ;
84- if ( propertyType == typeof ( bool ) )
85- propertyValue = Convert . ToBoolean ( stringValue ) ;
86- else if ( propertyType == typeof ( byte ) )
87- propertyValue = Convert . ToByte ( stringValue ) ;
88- else if ( propertyType == typeof ( sbyte ) )
89- propertyValue = Convert . ToSByte ( stringValue ) ;
90- else if ( propertyType == typeof ( short ) )
91- propertyValue = Convert . ToInt16 ( stringValue ) ;
92- else if ( propertyType == typeof ( ushort ) )
93- propertyValue = Convert . ToUInt16 ( stringValue ) ;
94- else if ( propertyType == typeof ( int ) )
95- propertyValue = Convert . ToInt32 ( stringValue ) ;
96- else if ( propertyType == typeof ( uint ) )
97- propertyValue = Convert . ToUInt32 ( stringValue ) ;
98- else if ( propertyType == typeof ( long ) )
99- propertyValue = Convert . ToInt64 ( stringValue ) ;
100- else if ( propertyType == typeof ( ulong ) )
101- propertyValue = Convert . ToUInt64 ( stringValue ) ;
102- else if ( propertyType == typeof ( float ) )
103- propertyValue = Convert . ToSingle ( stringValue ) ;
104- else if ( propertyType == typeof ( double ) )
105- propertyValue = Convert . ToDouble ( stringValue ) ;
106- else if ( propertyType == typeof ( decimal ) )
107- propertyValue = Convert . ToDecimal ( stringValue ) ;
108- else if ( propertyType == typeof ( DateTime ) )
109- propertyValue = Convert . ToDateTime ( stringValue ) ;
110- pi . SetValue ( this , propertyValue ) ;
109+ var value = queryString [ key ] ;
110+ LoadFromQueryString ( key , value ) ;
111111 }
112112 }
113113
@@ -118,14 +118,13 @@ public Uri ToUri(bool includePassword = false, bool includeOtherProperty = false
118118 HashSet < string > ignorePropertyNames = new HashSet < string > ( ) ;
119119 ignorePropertyNames . Add ( nameof ( HeartBeatInterval ) ) ;
120120 if ( ! includePassword )
121- ignorePropertyNames . Add ( nameof ( Password ) ) ;
122-
121+ ignorePropertyNames . Add ( nameof ( Password ) ) ;
123122 string baseUrl = ToUriBasic ( ignorePropertyNames ) ;
124123 if ( includePassword || includeOtherProperty )
125124 {
126125 StringBuilder sb = new StringBuilder ( baseUrl ) ;
127126 int currentIndex = 0 ;
128- var jObj = JsonNode . Parse ( JsonSerializer . Serialize ( this , this . GetType ( ) , GetJsonSerializerContext ( ) ) ) . AsObject ( ) ;
127+ var jObj = JsonNode . Parse ( JsonSerializer . Serialize ( this , GetType ( ) , GetJsonSerializerContext ( ) ) ) . AsObject ( ) ;
129128 foreach ( var property in jObj )
130129 {
131130 var key = property . Key ;
@@ -134,12 +133,14 @@ public Uri ToUri(bool includePassword = false, bool includeOtherProperty = false
134133 if ( ! includeOtherProperty && key != nameof ( Password ) )
135134 continue ;
136135 if ( currentIndex == 0 )
137- sb . Append ( "?" ) ;
136+ sb . Append ( '?' ) ;
138137 if ( currentIndex > 0 )
139- sb . Append ( "&" ) ;
138+ sb . Append ( '&' ) ;
140139 currentIndex ++ ;
141140
142- var value = property . Value . ToString ( ) ;
141+ var value = property . Value ? . ToString ( ) ;
142+ if ( string . IsNullOrEmpty ( value ) )
143+ continue ;
143144 value = System . Web . HttpUtility . UrlEncode ( value ) ;
144145 sb . Append ( $ "{ key } ={ value } ") ;
145146 }
@@ -151,7 +152,7 @@ public Uri ToUri(bool includePassword = false, bool includeOtherProperty = false
151152
152153 public override string ToString ( ) => ToUri ( ) . ToString ( ) ;
153154
154- private static Dictionary < string , Func < QpClientOptions > > schemaQpClientOptionsFactoryDict = new Dictionary < string , Func < QpClientOptions > > ( ) ;
155+ private static readonly Dictionary < string , Func < QpClientOptions > > schemaQpClientOptionsFactoryDict = new Dictionary < string , Func < QpClientOptions > > ( ) ;
155156
156157 public static void RegisterUriSchema ( string schema , Func < QpClientOptions > optionsFactory )
157158 {
0 commit comments