Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
328 views
in Technique[技术] by (71.8m points)

c# - Access to internal classes from another project

I'm wondering if it's possible to access internal class variables from other project in c#. I know that is impossible in regular use, I explain it below.

I have one project (P1 [class library]) containing those classes...

internal class Settings
{
    public static long size {get;set;}

    public static DoSomethingInternalOnly()
    {
    }
}

public class Program
{
    public static Main()
    {

    }
}

... and another one (P2) containing:

public class Program
{
    public static Main()
    {
        //P1.CopyOfSettings.size = 2048; ???
    }
}

In Settings class I store internal variables (and methods) for app which cannot be visible from other projects. But those settings need to be passed somehow from P2 to P1 so I need second class "Settings2" in P2 containing the same variables (variables only!) as "Settings" with public keyword.

I feel that creating several classes containing the same variables is a waste of time and makes code unreadable. Is there better way to accomplish this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use the InternalsVisibleTo attribute and provide the name of a specific assembly that can see the internal types in your assembly.

That being said.. I think you are bordering on over-designing this. If the Settings class belongs only to Assembly A... don't put it in Assembly B... put it in Assembly A.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...