2011-11-21 129 views
1

我有一些项目的解决方案。其中一个项目是我定义的主要项目,他的课程也有一个主要的方法。项目之间共享变量

在这个类中,我定义了一些属性public和static。我想要从其他项目文件访问此属性。例如:

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

我怎样才能做到这一点?

编辑:

如果我添加项目A项目B参考 “将这个项目作为参考,将有一个圆形的依赖。”

项目B含有一些其它文件,在项目需要一种所以不必项目B的参考。

+1

身在同一个解决方案项目?如果是只包含该项目作为参考,如果没有,你可以使用反射来加载DLL – Ivo

+0

的确是这样,但在B“使用眼镜蛇”告诉我,类型或命名空间无法找到。 – Manu

+0

你添加引用,是指项目没有异常编译 – Ivo

回答

5

在项目B中,添加对项目A的引用,并向项目B添加一个using Cobra语句,无论您想从Cobra(项目A)名称空间访问哪些内容。

+0

这是行不通的。我在尝试之前询问过这里。 B中的“使用Cobra”告诉我无法找到类型或名称空间。 – Manu

+3

这确实有效,如果不是,那么你还没有添加引用。 – CodeCaster

+0

当我尝试添加:“添加此项目作为参考,将有一个循环依赖。” – Manu

2

您需要添加到项目A的引用,项目B - 右键单击​​Solution Explorer中的项目节点上,选择引用,然后再工程项目A.

然后,您将有机会获得所有类型在项目A中。

请参阅MSDN上的this How To

+0

+1链接到一个如何做:) – Tim

0

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

using Cobra; 

并添加项目A作为项目B.

+0

当我尝试添加引用:“添加这个项目作为参考,将有一个圆形的依赖。” – Manu

+0

作为@CodeCaster在答复中说:那你已经有了一个参考项目,项目A,B,您不能这样做,因为错误状态。 – Tim

2

根据您的意见,以其他的答案听起来参考就像你的问题真的是你有一个循环依赖,你需要打破。一般做到这一点的方法是分解出的接口,并将其放置在第三个项目,这两个其他项目可以参考这样反而

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

@Manu,

可以通过反思。以下是解决您的问题的方法。

您已经创建了2个项目

项目B - 有命名空间“网”,班级“的HttpHandler”

A计划 - 有命名空间“眼镜蛇”,静态类“纲要”和具有参考项目B现在

你的问题是你需要不给项目A的参考项目B,因为这样的解决方案将不建,因为它会给循环引用错误访问项目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); 
      } 
     } 
    } 
} 


希望这是一些帮助。

问候,

萨马