How can I restrict public access modifier in referenced library?

瘦欲@ 提交于 2019-12-25 09:28:15

问题


I have two different libraries which refereed the base library called base.dll. One library using for web platforms another one using for windows platform (WPF). I have to restrict restrict one public property in WPF platform but I want to access it in web platform. How can achieve this? I am trying condition compilation but I don’t know how to do exactly. Please help me

For Ex:- Base library:

public Class CommonClass{
    public string CssClass
               {
            get;
            set;
                }
}

Referenced Library 1 (Web) – Referenced the base library, need to access CssClass in this library

Referenced Library 2: (WPF) - Referenced the base library, need to restrict CssClass in this library


回答1:


This actually sounds like a design flaw. You create a library and define which properties should be accessible by users of that library. Normally you don't hide specific properties from specific clients.

However, I think you can achieve what you want by making the property internal and use the InternalsVisibleToAttribute in your base.dll:

[assembly: InternalsVisibleTo("webplatformassembly")]

public Class CommonClass{
    internal string CssClass
    {
        get;
        set;
    }
}

This way no client can see that property, except those mentioned in a InternalsVisibleTo attribute.

See also: Friend Assemblies (C# and Visual Basic)



来源:https://stackoverflow.com/questions/44302553/how-can-i-restrict-public-access-modifier-in-referenced-library

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!