简体   繁体   中英

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.

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.

在项目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.

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

See this How To on 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,

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

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.

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