Share variables between projects

后端 未结 5 814
天命终不由人
天命终不由人 2021-01-05 22:04

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

相关标签:
5条回答
  • 2021-01-05 22:39

    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.

    0 讨论(0)
  • 2021-01-05 22:41

    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();
    }
    
    0 讨论(0)
  • 2021-01-05 22:42

    @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

    0 讨论(0)
  • 2021-01-05 22:50

    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.

    0 讨论(0)
  • 2021-01-05 22:51

    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.

    0 讨论(0)
提交回复
热议问题