简体   繁体   English

在项目之间共享变量

[英]Share variables between projects

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. 其中一个项目是我定义为main的项目,他的班级也有一个主要的方法。

Inside this class, I've defined some properties public and static. 在这个类中,我已经定义了一些属性public和static。 What I want to to is access to this properties from other project file. 我想要的是从其他项目文件访问此属性。 For example: 例如:

Project A: 项目A:

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

Project B: 项目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." 如果我在项目B中添加项目A作为参考: “添加此项目作为参考,将存在循环依赖。”

Project B contain some other files, so having a reference to Project B in Project A is needed. 项目B包含一些其他文件,因此需要在项目A中引用项目B.

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

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. 您需要将项目A的引用添加到项目B - 右键单击​​解决方案资源管理器中的项目节点,选择引用,然后选择项目,然后选择项目A.

You will then have access to all the types in Project A. 然后,您将可以访问项目A中的所有类型。

See this How To on MSDN. 该如何在MSDN上。

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();
}

@Manu, @Manu,

It is possible through reflection. 通过反思是可能的。 The following is the solution to your problem. 以下是您的问题的解决方案。

You have created 2 projects 您已创建了2个项目

Project B -- having namespace "Net", class "HttpHandler" 项目B - 名称空间为“Net”,类为“HttpHandler”

Project A -- having namespace "cobra", static class "Program" and having reference of Project B 项目A - 具有名称空间“cobra”,静态类“程序”并参考项目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. 现在您的问题是您需要访问项目B中的“程序”类而不将项目A引用到项目B,因为这样解决方案将不会构建,因为它将给出循环引用错误。

Check out the following 请查看以下内容

Project A 项目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 项目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 萨马

You need to add a using directive at the top of project B's file: 您需要在项目B的文件顶部添加using指令:

using Cobra;

And add project A as a reference in project B. 并在项目B中添加项目A作为参考。

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

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