[英]C# - Faster way to get set public static fields instead of using Reflection.SetValue / GetValue
I have a scenario where I need to change public static fields during runtime.我有一个场景,我需要在运行时更改公共静态字段。 I understand that I can do it through reflection as below to get set the public static field I want, but it is really slow.
我知道我可以通过如下反射来设置我想要的公共静态字段,但它真的很慢。
string typeName = "ABC";
string fieldName = "IsA";
Type.GetType(typeName ).GetField(fieldName ).SetValue(null, value);
var value = Type.GetType(typeName ).GetField(fieldName ).GetValue(null);
I would like to know is there any faster way to access such as using Reflection.Emit, Linq.Expression or other methods.我想知道有没有更快的访问方式,例如使用 Reflection.Emit、Linq.Expression 或其他方法。 As what I know currently most of them only support fields with an instance.
据我所知,目前他们中的大多数只支持带有实例的字段。
You can use expressions for this.您可以为此使用表达式。 You basically have three options:
你基本上有三个选择:
In your case it's a bit tricky to go for typed expressions.在您的情况下,使用类型表达式有点棘手。 I guess we cannot assume that all static properties will be of type
string
?我想我们不能假设所有静态属性都是
string
类型? The second option allows you to easily create a fast setter for any field type.第二个选项允许您轻松地为任何字段类型创建快速设置器。
Note that when compiling expressions, you must maintain a cache for the compiled delegates.请注意,在编译表达式时,您必须为已编译的委托维护一个缓存。 The compilation step is very expensive!
编译步骤非常昂贵!
class Program
{
public class Foo
{
public static string Name;
}
public static void Main()
{
var delegateCache = new Dictionary<(string, string), Delegate>();
var typeName = typeof(Foo).FullName;
var fieldName = "Name";
var key = (typeName, fieldName);
// Caching is crucial!
if (!delegateCache.TryGetValue(key, out var d))
{
d = CreateStaticSetter(typeName, fieldName);
delegateCache.Add(key, d);
}
// For a strongly typed delegate, we would use Invoke() instead.
d.DynamicInvoke("new value");
Console.WriteLine(Foo.Name);
}
private static Delegate CreateStaticSetter(string typeName, string fieldName)
{
var type = Type.GetType(typeName) ?? throw new ArgumentException();
var field = type.GetField(fieldName) ?? throw new ArgumentException();
var valueExp = Expression.Parameter(field.FieldType, "value");
var fieldExp = Expression.Field(null, field);
var assignExp = Expression.Assign(fieldExp, valueExp);
// TODO: Can be further optimized with a strongly typed delegate.
var expr = Expression.Lambda(assignExp, valueExp);
return expr.Compile();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.