asp.net C # Reflection set the value of an unknown type field

Source: Internet
Author: User
Tags datetime eval reflection

Only need to use Convert.changetype

C#

var eval = new DataTable ();
var objType = order. GetType ();
var propertyval = Objtype.getproperty (propertyname[1]);
if (propertyval!= null)
{
var val = propertyval.getvalue (order, NULL). ToString ();
TODO: The Magic Way
var result = Eval.compute (
$ ' {val} {action[' Expression ']. Value<string> ()} {action[' Value ']. Value<string> ()} ",
"");
Propertyval.setvalue (Order, Convert.changetype (result, propertyval.propertytype), NULL);
}

Want to know how the changetype is implemented? is actually encapsulating a bunch of judgments hhhh
C#


public static object ChangeType (object value, TypeCode TypeCode, IFormatProvider provider)
{
if (value = = NULL && (TypeCode = Typecode.empty | | typecode = = Typecode.string | | typecode = typecode.object))
{
return null;
}
IConvertible Convertible = value as IConvertible;
if (convertible = null)
{
throw New InvalidCastException (environment.getresourcestring ("invalidcast_iconvertible"));
}
Switch (TypeCode)
{
Case Typecode.empty:
{
throw New InvalidCastException (environment.getresourcestring ("Invalidcast_empty"));
}
Case Typecode.object:
{
return value;
}
Case Typecode.dbnull:
{
throw New InvalidCastException (environment.getresourcestring ("Invalidcast_dbnull"));
}
Case Typecode.boolean:
{
Return convertible. ToBoolean (provider);
}
Case Typecode.char:
{
Return convertible. ToChar (provider);
}
Case Typecode.sbyte:
{
Return convertible. ToSByte (provider);
}
Case Typecode.byte:
{
Return convertible. ToByte (provider);
}
Case TYPECODE.INT16:
{
Return convertible. ToInt16 (provider);
}
Case TYPECODE.UINT16:
{
Return convertible. ToUInt16 (provider);
}
Case TYPECODE.INT32:
{
Return convertible. ToInt32 (provider);
}
Case TYPECODE.UINT32:
{
Return convertible. ToUInt32 (provider);
}
Case Typecode.int64:
{
Return convertible. ToInt64 (provider);
}
Case Typecode.uint64:
{
Return convertible. ToUInt64 (provider);
}
Case Typecode.single:
{
Return convertible. ToSingle (provider);
}
Case Typecode.double:
{
Return convertible. ToDouble (provider);
}
Case Typecode.decimal:
{
Return convertible. ToDecimal (provider);
}
Case Typecode.datetime:
{
Return convertible. ToDateTime (provider);
}
Case Typecode.object | Typecode.datetime:
{
throw new ArgumentException (environment.getresourcestring ("Arg_unknowntypecode"));
}
Case typecode.string:
{
Return convertible. ToString (provider);
}
Default
{
throw new ArgumentException (environment.getresourcestring ("Arg_unknowntypecode"));
}
}
}
The above code does not know that the friends did not understand oh, do it is very simple to judge a condition.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.