[英]How to access the internal properties of one assembly from another assembly without using InternalVisibleTo?
Without using the signing key (.snk) is there a way to access the internal property of one assembly from another assembly ? 不使用签名密钥(.snk),是否可以从另一个程序集中访问一个程序集的内部属性?
Thanks in advance. 提前致谢。 Arun 阿伦
Reflection when used through strongly typed delegates isn't TOO much slow... (it should be just a little slower than virtual
method calls): 通过强类型委托使用反射时不会太慢...(应该比virtual
方法调用慢一点):
Example: 例:
public class MyClass
{
internal int MyProperty { get; set; }
}
And then: 接着:
public static class MyClassAccessor
{
public static readonly Func<MyClass, int> GetMyProperty;
public static readonly Action<MyClass, int> SetMyProperty;
static MyClassAccessor()
{
var prop = typeof(MyClass).GetProperty("MyProperty", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
GetMyProperty = (Func<MyClass, int>)Delegate.CreateDelegate(typeof(Func<MyClass, int>), prop.GetMethod);
SetMyProperty = (Action<MyClass, int>)Delegate.CreateDelegate(typeof(Action<MyClass, int>), prop.SetMethod);
}
}
And you use like: 和你这样使用:
var mc = new MyClass();
MyClassAccessor.SetMyProperty(mc, 5);
Console.WriteLine(MyClassAccessor.GetMyProperty(mc));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.