c# - How can I fix this up to do generic conversion to Nullable<T>? -
I currently use this kind of conversion conversion method to convert between types:
< Pre> to public t & lt; T & gt; (This IConvertible obj) {Return (T) Convert. ChangeType (obj, typeof (T)); } However, it does not like to change valid values for Nullable, for example, it fails:
"1". & Lt; Int? & Gt; ; (); Obviously, 1 is easily converted to one (int?), But this error happens:
'system.String' From 'System.Nullable `1 [[System.Int32, mscorlib, version = 2.0.0.0, culture = neutral, public keycon = B77A5C 561934A98]]. This is a simple example, in fact, I'm using it to convert to string type:
packageDb.Quantity = package Package. Element dip (namespace + "packagequality", namespace + "realquarter", namespace + "quantity"). Price Ornol () & Lt; Int? & Gt; (); If Convert.ChangeType does not like Nailble, then does anyone have any great ideas?
public from stable t & lt; T & gt; (This is IConvertible obj) {type T = typef (T); Type U = Null. GETUnderlyingType (t); If (u! = Null) {return (obj == faucet)? Default (T): (T) Convert.Change Type (obj, u); } Other {Return (T) Convert. Change type (obj, t); }}
Comments
Post a Comment