C#命名空间数据共享

时间:2010-07-19 12:08:59

标签: c#

我是.Net C#编码的新手。我在XML文件中读取了我的aspx.cs文件,我希望使用这些数据在silverlight应用程序中创建一个图表,该应用程序是同一项目的一部分,但aspx c#网站和silverlight应用程序具有不同的命名空间。如何将我的XML读取数据发送到同一项目但不同名称空间的silverlight应用程序?

1 个答案:

答案 0 :(得分:3)

命名空间不提供任何数据边界;它只是名称的一部分。因此,如果您在项目中定义了类,那么:

namespace Some.Namespace
{
    class FirstClass 
    {
        string SomeValue { get; set; }
    }
}

...然后想在同一个项目的不同命名空间中的另一个类中使用这个类,你只需要使用命名空间引用FirstClass

namespace Some.Other.Namespace
{
    class SecondClass
    {
        void SomeMethod()
        {
            Some.Namespace.FirstClass temp = new Some.Namespace.FirstClass();
            temp.SomeValue = "test";
        }
    }
}

一种替代方案,可以让您的代码更加简洁,就是在using的代码文件中添加SecondClass指令,这样您就可以引用FirstClass而不包括命名空间每次:

using Some.Namespace;

namespace Some.Other.Namespace
{
    class SecondClass
    {
        void SomeMethod()
        {
            // note how the namespace is not needed here:
            FirstClass temp = new FirstClass();
            temp.SomeValue = "test";
        }
    }
}

这意味着如果你有一个处理访问xml文件的类,并且它位于项目的某个命名空间中,那么你应该能够在aspx页面和silverlight应用程序中使用同一个类。

相关问题