简体   繁体   English

如何在不使用InternalVisibleTo的情况下从另一个程序集访问一个程序集的内部属性?

[英]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.

相关问题 CodeContracts静态检查说,即使使用InternalVisibleTo,另一个程序集的内部类的成员也不够明显 - CodeContracts static checking says the members of an internal class of another assembly are not visible enough, despite using InternalVisibleTo 如何使用“InternalsVisibleTo”从 XAML 访问另一个程序集中的内部类转换器 - How to access to Internal class converter in another assembly from XAML with "InternalsVisibleTo" 如何从另一个程序集访问内部静态类? - How can I access to an internal static class from another assembly? 可以在C#中使用InternalVisibleTo而不使用强名称程序集吗? - Can one use InternalVisibleTo without strong-named assembly in C#? 如何从另一个程序集中的内部接口派生 - How to derive from an internal interface in another assembly 如何测试属性是否来自另一个程序集 - how to test if properties is from another assembly 如何在C#中从外部程序集访问“内部”修饰符类属性 - How to access “internal” modifier class properties from external assembly in C# 如何从外部程序集访问内部类? - How can I access an internal class from an external assembly? 如何从另一个程序集访问ApplicationSettings部分? - How do I access the ApplicationSettings section from another assembly? 无法访问另一个程序集中的公共类型成员。 我有两个程序集,我想从另一个程序集中的一个程序集访问公共成员 - Unable to access public type members in another assembly. I have two assemblies, I want to access public members from one assembly in another assembly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM