在项目之间共享变量

发布于 2024-12-17 10:31:28 字数 762 浏览 0 评论 0原文

我对一些项目有一个解决方案。其中一个项目是我定义为 main 的项目,他的类也有一个 main 方法。

在这个类中,我定义了一些公共和静态属性。我想要的是从其他项目文件访问此属性。例如:

项目 A:

namespace Cobra
{
    public static class Program
    {
        public static int A;
        public static int B;
...

项目 B:

namespace Net
{
    public class HttpHandler : IHttpHandler
    {
        ...
        public void ProcessRequest()
        int a =Cobra.Program.A;
        int b =Cobra.Program.B;
...

我该怎么做?

编辑:

如果我在项目 B 中添加项目 A 作为参考:“添加此项目作为参考,将会出现一个循环依赖关系。”

项目 B 包含一些其他文件,因此需要在项目 A 中引用项目 B

I have a solution with some projects. One of this projects is the one I've defined as main, also his class has a main Method.

Inside this class, I've defined some properties public and static. What I want to to is access to this properties from other project file. For example:

Project A:

namespace Cobra
{
    public static class Program
    {
        public static int A;
        public static int B;
...

Project B:

namespace Net
{
    public class HttpHandler : IHttpHandler
    {
        ...
        public void ProcessRequest()
        int a =Cobra.Program.A;
        int b =Cobra.Program.B;
...

How can I do this??

EDIT:

If I add Project A as reference in Project B: "Adding this project as a reference, there will be a circular dependency."

Project B contain some other files, so having a reference to Project B in Project A is needed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

我不是你的备胎 2024-12-24 10:31:28

在项目 B 中,添加对项目 A 的引用,并在您想要从 Cobra(项目 A)命名空间访问某些内容的任何位置添加 using Cobra 语句到项目 B。

In Project B, Add a reference to Project A and add a using Cobra statement to Project B wherever you want to access something from the Cobra (Project A) namespace.

半城柳色半声笛 2024-12-24 10:31:28

您需要将对项目 A 的引用添加到项目 B - 右键单击​​解决方案资源管理器中的项目节点,选择引用,然后选择项目,然后选择项目 A。

然后您将可以访问项目 A 中的所有类型。

请参阅 MSDN 上的此操作方法

You need to add a reference to Project A to project B - right click on the project node in the solution explorer, select references, then projects then Project A.

You will then have access to all the types in Project A.

See this How To on MSDN.

北斗星光 2024-12-24 10:31:28

根据您对其他答案的评论,听起来您的问题实际上是您需要打破循环依赖关系。一般来说,这样做的方法是分解一个接口并将其放置在其他两个项目都可以引用的第三个项目中,这样

class Foo //foo lives in project 1 (depends on project 2)
{
    public Bar GetNewBar()
    {
        return new Bar(this);
    }
    public void DoSomething(){}
}

public class Bar //bar lives in project 2 (depends on project 1 -- cant do this)
{
    public Bar(Foo parent){}
}

您就不需要

class Foo: IFoo //foo lives in project 1 (depends on project 2 and 3)
{
    public Bar GetNewBar()
    {
        return new Bar(this);
    }
    public void DoSomething(){}
}

public class Bar //bar lives in project 2 (depends on project 3)
{
    public Bar(IFoo parent){}
}

public interface IFoo //IFoo lives in project 3 (depends on nothing)
{
    void DoSomething();
}

Based on your comments to other answers it sounds like your problem is really that you have a circular dependency which you need to break. Generally the way to do that is to factor out an interface and place it in a third project that both other projects can reference so instead of

class Foo //foo lives in project 1 (depends on project 2)
{
    public Bar GetNewBar()
    {
        return new Bar(this);
    }
    public void DoSomething(){}
}

public class Bar //bar lives in project 2 (depends on project 1 -- cant do this)
{
    public Bar(Foo parent){}
}

you have

class Foo: IFoo //foo lives in project 1 (depends on project 2 and 3)
{
    public Bar GetNewBar()
    {
        return new Bar(this);
    }
    public void DoSomething(){}
}

public class Bar //bar lives in project 2 (depends on project 3)
{
    public Bar(IFoo parent){}
}

public interface IFoo //IFoo lives in project 3 (depends on nothing)
{
    void DoSomething();
}
書生途 2024-12-24 10:31:28

@Manu,

通过反射是可能的。以下是您问题的解决方案。

您已经创建了 2 个项目

项目 B — 具有命名空间“Net”、类“HttpHandler”

项目 A — 具有命名空间“cobra”、静态类“Program”并具有项目 B 的引用

现在您的问题是您需要访问该类在项目 B 中“编程”而不将项目 A 的引用提供给项目 B,因为这样解决方案将无法构建,因为它将给出循环引用错误。

查看以下

项目A

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Net;

namespace Cobra
{
    public static class Program
    {
        public static int A { get; set; }//Getter/Setter is important else "GetProperties" will not be able to detect it
        public static int B { get; set; }

        static void Main(string[] args)
        {
            HttpHandler obj = new HttpHandler();
            obj.ProcessRequest(typeof(Program));
        }
    }
}


项目B

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace Net
{
    public class HttpHandler : IHttpHandler 
    {
        public void ProcessRequest(Type cobraType)
        {
            int a, b;
            foreach (var item in cobraType.GetProperties(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy))
            {
                if (item.Name == "A")
                    a = (int)item.GetValue(null, null);//Since it is a static class pass null
                else if (item.Name == "B")
                    b = (int)item.GetValue(null, null);
            }
        }
    }
}


希望这对您有所帮助。

问候,

萨马尔

@Manu,

It is possible through reflection. The following is the solution to your problem.

You have created 2 projects

Project B -- having namespace "Net", class "HttpHandler"

Project A -- having namespace "cobra", static class "Program" and having reference of Project B

Now your problem is you need to access the class "Program" in Project B without giving reference of Project A to Project B because then the solution will not build as it will give cyclic reference error.

Check out the following

Project A

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Net;

namespace Cobra
{
    public static class Program
    {
        public static int A { get; set; }//Getter/Setter is important else "GetProperties" will not be able to detect it
        public static int B { get; set; }

        static void Main(string[] args)
        {
            HttpHandler obj = new HttpHandler();
            obj.ProcessRequest(typeof(Program));
        }
    }
}


Project B

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace Net
{
    public class HttpHandler : IHttpHandler 
    {
        public void ProcessRequest(Type cobraType)
        {
            int a, b;
            foreach (var item in cobraType.GetProperties(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy))
            {
                if (item.Name == "A")
                    a = (int)item.GetValue(null, null);//Since it is a static class pass null
                else if (item.Name == "B")
                    b = (int)item.GetValue(null, null);
            }
        }
    }
}


Hope this is of some help.

Regards,

Samar

铃予 2024-12-24 10:31:28

您需要在项目 B 的文件顶部添加一条 using 指令:

using Cobra;

并将项目 A 添加为项目 B 中的引用。

You need to add a using directive at the top of project B's file:

using Cobra;

And add project A as a reference in project B.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文