简体   繁体   中英

conditional EditorBrowsable by web.config AppSettings

I have a Project A that will be used as a reference in Project B .

Project A has a class with a method that should be either visible or not depending on a condition specified in Project B . Say the method would be visible if in the web.config appSettings of the project B there was a key "DisplayMethod" with a value "true".

This is more of a version control. I don't want to create a separate dll for a new version of the Project A. I'd like a single dll to be able to support both old and new version. This way developer of Project B can easily switch version in web.config, rebuild project and he is good to go.

Restrictions

  • I don't have control over Project B. I can only ask developer of Project B for a simple modification like web.config modification.
  • I have no idea what projects will be using Project A. Project A is an open source project and can be used by anybody, potentially in thousands of projects.

By "method would be visible" I mean the same functionality as the following code would do:

[EditorBrowsable(EditorBrowsableState.Never)]
public string SomeMethod(string s)
{
    return "Hello " + s;
}

Research so far:

I've seen this question . However the accepted answer to it does not work for me since the conditional constant there can only be set in the project properties of the same project where the method is.

Conditional namespace would do as well, but there I face the same issues.

Values for attributes must be known by compiler at compile time. so what you want to achive is impossible. im not expert about it but maybe Mono Cecil is capable of that. though im not sure its worth it.

If by "visible to", you mean "only accessible by", then you may be able to leverage friend assemblies . It lets another assembly access methods/propreties/fields, etc. that are marked as internal.

In order for this to work, you need to declare that B is a friend of A at compile time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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