从另一个项目访问内部类 [英] Access to internal classes from another project

查看:63
本文介绍了从另一个项目访问内部类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有可能从c#中的其他项目访问内部类变量.我知道在常规使用中这是不可能的,我在下面说明.

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.

我有一个包含这些类的项目(P1 [类库])...

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()
    {

    }
}

...和另外一个(P2)包含:

... and another one (P2) containing:

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

在Settings类中,我存储了应用程序的内部变量(和方法),这些变量在其他项目中不可见.但是这些设置需要以某种方式从P2传递到P1,因此我需要P2中的第二类"Settings2"包含与带有public关键字的"Settings"相同的变量(仅变量!).

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?

推荐答案

您可以使用

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

话虽这么说..我认为您正在过度设计它.如果 Settings 类仅属于程序集A ...请不要将其放在程序集B中...应将其放在程序集A中.

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.

这篇关于从另一个项目访问内部类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆