简体   繁体   English

如何限制引用库中的公共访问修饰符?

[英]How can I restrict public access modifier in referenced library?

I have two different libraries which refereed the base library called base.dll. 我有两个不同的库,它们引用了称为base.dll的基础库。 One library using for web platforms another one using for windows platform (WPF). 一个库用于Web平台,另一个库用于Windows平台(WPF)。 I have to restrict restrict one public property in WPF platform but I want to access it in web platform. 我必须限制在WPF平台中限制一个公共财产,但我想在Web平台中访问它。 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: 对于Ex:-基本库:

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

Referenced Library 1 (Web) – Referenced the base library, need to access CssClass in this library 引用的库1(Web)–引用的基础库,需要访问此库中的CssClass

Referenced Library 2: (WPF) - Referenced the base library, need to restrict CssClass in this library 引用的库2:(WPF)-引用的基本库,需要限制此库中的CssClass

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 : 但是,我认为您可以通过将属性设置为internal并在base.dll使用InternalsVisibleToAttribute来实现所需的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. 这样,除了InternalsVisibleTo属性中提到的那些属性外,任何客户端都看不到该属性。

See also: Friend Assemblies (C# and Visual Basic) 另请参见: 朋友程序集(C#和Visual Basic)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM