{
return EmitTest.GetEmitInvoker(type, propertyName)(instance); }
//Use the cached dynamic method to get the property’s value
public static object GetValueEmitWithCache(Type type, string propertyName, object instance)
{
GetPropertyValueInvoker invoker = null;
if (!_invokerCache.TryGetValue(propertyName, out invoker))
{
invoker = EmitTest.GetEmitInvoker(type, propertyName);
_invokerCache[propertyName] = invoker;
}
return invoker(instance);
}
//Use reflection to get the property’s value
public static object GetValueReflect(Type type, string propertyName, object instance)
{
PropertyInfo propInfo = type.GetProperty(propertyName
, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
return propInfo.GetValue(instance, null);
}
}
测试用的实体类:
public class TestEntity
{
private string _string1;
private int _int1;
private DateTime _datetime1;
public string String1
{
get { return this._string1; }
set { this._string1 = value; }
}
public int Int1
{
get { return this._int1; }
set { this._int1 = value; }
}
public DateTime Datetime1
{
get { return this._datetime1; }
set { this._datetime1 = value; }
}
}
测试代码:
TestEntity entity1 = new TestEntity();
entity1.String1 = \"a string field\";
entity1.Int1 = 123;
entity1.Datetime1 = DateTime.Now;
延伸阅读
文章来源于领测软件测试网 https://www.ltesting.net/