Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C语言中访问/创建多个文件中一个类的对象_C#_Asp.net - Fatal编程技术网

C# 在C语言中访问/创建多个文件中一个类的对象

C# 在C语言中访问/创建多个文件中一个类的对象,c#,asp.net,C#,Asp.net,我有几节课,比如说 public class A { public string Date { get; set; } public string Name { get; set; } public string Description { get; set; } public string Link { get; set; } public string Document { get; set; } } public class B { pub

我有几节课,比如说

public class A
{
    public string Date { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public string Link { get; set; }
    public string Document { get; set; }
}

public class B
{
    public string Fixes { get; set; }
    public string Date { get; set; }
    public string Link { get; set; }
    public string Document { get; set; }
}
我在两个文件Webforms中使用这两个类,如下所示

var allVars= (from p in doc.Elements("Patches").Elements("Patch").Elements("Fix")
                      select new B
                      {
                          Fixes = p.Value,
                          Date = (p.Parent.Attribute("date") == null) ? "NA" : p.Parent.Attribute("date").Value,
                          Link = (p.Attribute("Ticket") == null) ? "" : p.Attribute("Ticket").Value,
                          Document = (p.Attribute("Document") != null) ? p.Attribute("Document").Value : ""
                      }
                          );

 var allVariables= (from f in doc.Elements("Features").Elements("Feature")
                       select new A
                       {
                           Date=(f.Parent.Attribute("date") == null) ? "NA" : f.Parent.Attribute("date").Value,
                           Name = f.Attribute("name").Value,
                           Description = f.Value,
                           Link = f.Attribute("Ticket").Value,
                           Document = (f.Attribute("Document") != null) ? f.Attribute("Document").Value : null
                       });
因此,在我的应用程序中,在多个WebFormSpatial类中使用这些类。而不是在每个类中分别定义它们。我希望它们定义一次,并在任何文件中使用它们,就像那样。我确信C会有这样的机制,但我记不起特性名称以及如何重用它


有人能告诉我这是如何实现的。

将其定义为这两个类中的静态函数:

公共A类 { 公共字符串日期{get;set;} 公共字符串名称{get;set;} 公共字符串说明{get;set;} 公共字符串链接{get;set;} 公共字符串文档{get;set;} 公共静态动态获取 { var allVariables=来自doc.ElementsFeatures.ElementsFeature中的f 选择一个新的 { 日期=f.Parent.Attributedate==null?NA:f.Parent.Attributedate.Value, Name=f.Attributename.Value, 描述=f.值, Link=f.attributecket.Value, Document=f.AttributeDocument!=null?f.AttributeDocument.Value:null }; 返回所有变量; } } 公共B级 { 公共字符串修复{get;set;} 公共字符串日期{get;set;} 公共字符串链接{get;set;}ynamig 公共字符串文档{get;set;} 公共静态动态getB { var allVars=来自doc.ElementsPatches.ElementsPatch.ElementsFix中的p 选择新的B { 固定=p.值, 日期=p.Parent.Attributedate==null?NA:p.Parent.Attributedate.Value, Link=p.attributecket==null?:p.attributecket.Value, Document=p.AttributeDocument!=null?p.AttributeDocument.Value: } ; 返回所有变量; }
}编写一个方法并从窗体中调用该方法?您是说我需要编写一个方法来执行第二个块中的方法,并在包含类定义的公共文件中定义该方法并从窗体中调用它?我们不能在这里使用名称空间吗?在命名空间中的.cs文件中定义类,在webforms中使用该命名空间并使用using指令?这些类的定义应在名为xyz.cs的.cs文件中。那么我们的Web表单文件如何知道如何使用这些文件中的类呢?我说的是C++中的include只是将xyz.cs添加到项目中。如果需要在多个项目中使用/引用它,请将xyz.cs编译为一个单独的项目类库,并将对此新程序集的引用添加到任何必须使用它的项目中。在项目中创建新类并将该代码添加到其中,确保它们与项目具有相同的命名空间