代替タイトル:実行時に型に動的に変換します。
オブジェクトを、実行時に割り当てられる型に変換したいと考えています。
TextBox or Dropdownlist
たとえば、文字列値 ( から) を に代入する関数があるとしObject.Property
ます。
値を適切な型に変換するにはどうすればよいですか? たとえば、整数、文字列、または列挙型の可能性があります。
Public void Foo(object obj,string propertyName,object value)
{
//Getting type of the property og object.
Type t= obj.GetType().GetProperty(propName).PropertyType;
//Now Setting the property to the value .
//But it raise an error,because sometimes type is int and value is "2"
//or type is enum (e.a: Gender.Male) and value is "Male"
//Suppose that always the cast is valid("2" can be converted to int 2)
obj.GetType().GetProperty(propName).SetValue(obj, value, null);
}