Tag: getproperty

在C#.NET中即时转换为Type

我想将Object转换为将在运行时分配的类型。 例如,假设我有一个函数将字符串值(从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 (ea: Gender.Male) and value is […]

使用GetProperty获取子属性的最佳方法

public class Address { public string ZipCode {get; set;} } public class Customer { public Address Address {get; set;} } 如何通过reflection访问“ZipCode”或“Address.ZipCode”? 例如: Typeof(Customer).GetProperty(“ZipCode”)?