Tag: system.reflection

使用Reflection在对象初始化之前设置静态变量值?

无论如何设置尚未初始化的对象上的静态(私有)变量的值? SetValue方法需要一个实例,但我希望有办法解决这个问题。

如何将PropertyInfo转换为属性表达式并使用它来调用generics方法?

如何将PropertyInfo转换为属性表达式,可用于调用StructuralTypeConfiguration.Ignore(Expression<Func> propertyExpression)方法? 我尝试使用Expression.Property()来构造表达式,但是当我将此表达式用作propertyExpression参数时,我收到以下错误: The type arguments for method cannot be inferred from the usage. Try specifying the type arguments explicitly. 此错误可能是指TProperty类型参数,我不知道如何指定只有PropertyInfo 。 我这样做是关于: 使用entity framework的StructuralTypeConfiguration.Ignore()忽略所有属性,但指定set 。 UPDATE 代码不起作用: var propertyInfo = typeof(Foo).GetProperties()[0]; var expression = Expression.Default(typeof(Foo)); var expressionProperty = Expression.Property(expression, propertyInfo); Ignore(expressionProperty);

我需要一个永远不会返回null的`Assembly.GetEntryAssembly()`的替代品

我需要找到托管代码执行开始的程序集。 // using System.Reflection; Assembly entryAssembly = Assembly.GetEntryAssembly(); 这似乎是要走的路,但Assembly.GetEntryAssembly的MSDN参考页指出此方法“[c]从非托管代码调用时返回null。” 在这种情况下,我想知道哪个程序集是由非托管代码调用的。 有没有一种可靠的方法,即总是返回非空的Assembly引用?

使用reflection获取方法名称和参数

我正在尝试以一种方式编写一个基于方法名称和参数以编程方式为Memcached创建密钥的方法。 所以,如果我有一个方法, string GetName(int param1, int param2); 它会返回: string key = “GetName(1,2)”; 我知道您可以使用reflection获取MethodBase,但是如何获取字符串中的参数值,而不是参数类型?