C#高性能动态获取对象属性值

动态获取对象的性能值,这个在开发过程中经常会遇到,这里我们探讨一下何如高性能的获取属性值。为了对比测试,我们定义一个类People

public class People
{
    public string Name { get; set; }
}
 然后通过直接代码调用方式来取1千万次看要花多少时间:
private static void Directly()
{
    People people = new People { Name = "Wayne" };
    Stopwatch stopwatch = Stopwatch.StartNew();
    for (int i = 0; i < 10000000; i++)
    {
        object value = people.Name;
    }
    stopwatch.Stop();
    Console.WriteLine("Directly: {0}ms", stopwatch.ElapsedMilliseconds);
}

大概花了37ms:

反射

通过反射来获取对象的属性值,这应该是大家常用的方式,但这种方式的性能比较差。接下来我们来看看同样取1千万次需要多少时间:

private static void Reflection()
{
    People people = new People { Name = "Wayne" };
    Type type = typeof(People);
    PropertyInfo property = type.GetProperty("Name");
    Stopwatch stopwatch = Stopwatch.StartNew();
    for (int i = 0; i < 10000000; i++)
    {
        object value = property.GetValue(people);
    }
    stopwatch.Stop();
    Console.WriteLine("Reflection: {0}ms", stopwatch.ElapsedMilliseconds);
}
 大概花了1533ms,果然要慢很多:

那既然反射慢,那还有没有其它方式呢?

动态构建Lambda

我们知道可以动态构建Linq的Lambda表达式,然后通过编译后得到一个委托,如果能动态构建返回属性值的委托,就可以取到值了。所以我们想办法构建一个像这样的委托:

Func<People, object> getName = m => m.Name;
 接下来我们就通过Expression来构建:
private static void Lambda()
{
    People people = new People { Name = "Wayne" };
    Type type = typeof(People);
    var parameter = Expression.Parameter(type, "m");//参数m
    PropertyInfo property = type.GetProperty("Name");
    Expression expProperty = Expression.Property(parameter, property.Name);//取参数的属性m.Name
    var propertyDelegateExpression = Expression.Lambda(expProperty, parameter);//变成表达式 m => m.Name
    var propertyDelegate = (Func<People, object>)propertyDelegateExpression.Compile();//编译成委托
    Stopwatch stopwatch = Stopwatch.StartNew();
    for (int i = 0; i < 10000000; i++)
    {
        object value = propertyDelegate.Invoke(people);
    }
    stopwatch.Stop();
    Console.WriteLine("Lambda:{0}ms", stopwatch.ElapsedMilliseconds);
}
 然后我们测试一下,大概花了138ms,性能要比反射好非常多:

委托调用

虽然动态构建Lambda的性能已经很好了,但还是更好吗?毕竟比直接调用还是差了一些,要是能直接调用属性的取值方法就好了。

在C#中,可读属性都有一个对应的get_XXX()的方法,可以通过调用这个方法来取得对应属性的值。可以使用System.Delegate.CreateDelegate创建一个委托来调用这个方法。

  • 通过委托调用方法来取得属性值

我们定义一个MemberGetDelegate的委托,然后通过它来调用取值方法:

delegate string MemberGetDelegate(People p);
private static void Delegate()
{
    People people = new People { Name = "Wayne" };
    Type type = typeof(People);
    PropertyInfo property = type.GetProperty("Name");
    MemberGetDelegate memberGet = (MemberGetDelegate)System.Delegate.CreateDelegate(typeof(MemberGetDelegate), property.GetGetMethod());
    Stopwatch stopwatch = Stopwatch.StartNew();
    for (int i = 0; i < 10000000; i++)
    {
        object value = memberGet(people);
    }
    stopwatch.Stop();
    Console.WriteLine("Delegate: {0}ms", stopwatch.ElapsedMilliseconds);
}
 然后我们测试一下,大概花了38ms,性能几乎与直接调用一致:

但这个方法的限制在于需要知道返回类型,在返回类型未知的时候就显得不那么方便了。

Emit

Emit可以在运行时动态生成代码,我们可以动态构建一个方法,在这个方法里面调用取属性值的方法:

private static void Emit()
{
    People people = new People { Name = "Wayne" };
    Type type = typeof(People);
    var property = type.GetProperty("Name");
    DynamicMethod method = new DynamicMethod("GetPropertyValue", typeof(object), new Type[] { type }, true);
    ILGenerator il = method.GetILGenerator();
    il.Emit(OpCodes.Ldarg_0);
    il.Emit(OpCodes.Callvirt, property.GetGetMethod());
            
    if (property.PropertyType.IsValueType)
    {
        il.Emit(OpCodes.Box, property.PropertyType);//值类型需要装箱,因为返回类型是object
    }
    il.Emit(OpCodes.Ret);
    Func<People, object> fun = method.CreateDelegate(typeof(Func<People, object>)) as Func<People, object>;

    Stopwatch stopwatch = Stopwatch.StartNew();
    for (int i = 0; i < 10000000; i++)
    {
        object value = fun.Invoke(people);
    }
    stopwatch.Stop();
    Console.WriteLine("Emit:{0}ms", stopwatch.ElapsedMilliseconds);
}
 测试一下,大概119ms,性能与动态构建Lambda编译的委托接近

 

文章转载自 http://www.zkea.net/codesnippet/detail/csharp-fast-get-property-value.html

posted @ 2020-12-10 11:54  浅巷深念  阅读(453)  评论(0编辑  收藏  举报