Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.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#_Oop_Class - Fatal编程技术网

C# 如何在另一个类中的类中添加数据

C# 如何在另一个类中的类中添加数据,c#,oop,class,C#,Oop,Class,我有一门课,如下所示。我想为一个Web服务发送一个类似这样的Messageformat,因此我需要向Attributecollection和Input添加值 public class Messageformat { public class Attributecollection { public string name { get; set; } public int id { get; set; } } public cla

我有一门课,如下所示。我想为一个Web服务发送一个类似这样的
Messageformat
,因此我需要向
Attributecollection
Input
添加值

public class Messageformat
{
    public class Attributecollection
    {
        public string name { get; set; }

        public int id { get; set; }
    }

    public class Input
    {
        public string soid { get; set; }

        public string itemname { get; set; }

        public int qty { get; set; }
    }

    public class output
    {
        public string soid { get; set; }
    }
}

我想给这个类添加值。我一次只能调用一个类并向该类添加值。是否有任何方法可以调用类
Messageformat
,并通过该对象向子类添加值。我知道它是一个嵌套的类结构。我不擅长oop,所以有人能分享一些关于这方面的想法吗?

你为什么这样做?不要嵌套类,而是在同一级别声明类,并在其他类的
MessageFormat
中创建属性,例如

  public class Messageformat
  {
     public Attributecollection AtrributeCollection{get;set;}
     public Input Input{get;set;}
     public output Output{get;set;}
  }
  public class Attributecollection
  {
     public string name { get; set; }
     public int id { get; set; }
  }
  public class Input
  {
     public string soid { get; set; }
     public string itemname { get; set; }
     public int qty { get; set; }
  }
  public class output
  {
     public string soid { get; set; }
  }
通过这种方式,您可以轻松创建MessageFormat对象并插入所有其他三个类的数据,即

  [WebMethod]
  public string GetMessage()
  {
        Messageformat msgFrmt = new Messageformat();
        msgFrmt.AtrributeCollection = new Atrributecollection()
                                         { 
                                             name="test",
                                             id=1
                                         };

        msgFrmt.Input = new Input()
                            {
                               soid= "soid value",
                               itemname="item",
                               qty=10
                            };
        msgFrmt.Output = new output()
                             {
                                soid="soid value"
                             };

        return new JavaScriptSerializer().Serialize(msgFrmt);
  }

上面是一个简单的例子,说明如何组织类并将它们用于任何目的。(上面是webmethod的一个例子,但是你可以做任何你想做的事情)

你为什么这样做?不要嵌套类,而是在同一级别声明类,并在其他类的
MessageFormat
中创建属性,例如

  public class Messageformat
  {
     public Attributecollection AtrributeCollection{get;set;}
     public Input Input{get;set;}
     public output Output{get;set;}
  }
  public class Attributecollection
  {
     public string name { get; set; }
     public int id { get; set; }
  }
  public class Input
  {
     public string soid { get; set; }
     public string itemname { get; set; }
     public int qty { get; set; }
  }
  public class output
  {
     public string soid { get; set; }
  }
通过这种方式,您可以轻松创建MessageFormat对象并插入所有其他三个类的数据,即

  [WebMethod]
  public string GetMessage()
  {
        Messageformat msgFrmt = new Messageformat();
        msgFrmt.AtrributeCollection = new Atrributecollection()
                                         { 
                                             name="test",
                                             id=1
                                         };

        msgFrmt.Input = new Input()
                            {
                               soid= "soid value",
                               itemname="item",
                               qty=10
                            };
        msgFrmt.Output = new output()
                             {
                                soid="soid value"
                             };

        return new JavaScriptSerializer().Serialize(msgFrmt);
  }

上面是一个简单的例子,说明如何组织类并将它们用于任何目的。(上面是webmethod的一个示例,但是您可以做任何您想做的事情)

您的问题不是很清楚,但是如果您要问如何填充数据,您必须将所谓的属性添加到父类中。请注意,您不必使用内部类

public class Attributecollection
{
    public string name { get; set; }
    public int id { get; set; }
}

public class Input
{
    public string soid { get; set; }
    public string itemname { get; set; }
    public int qty { get; set; }
}

public class output
{
    public string soid { get; set; }
}

public class Messageformat
{
    public Attributecollection MyAttributecollection { get; set; }
    public Input MyInput { get; set; }
    public output Myoutput { get; set; }
}

...

Messageformat test = new Messageformat
    {
        MyAttributecollection = new Attributecollection { name = "", id = 1 },
        MyInput = new Input { soid = "", itemname ="", qty = 1 },
        Myoutput = new output { soid = "" }
    };

您的问题不是很清楚,但如果您询问如何填充数据,则必须将所谓的属性添加到父类中。请注意,您不必使用内部类

public class Attributecollection
{
    public string name { get; set; }
    public int id { get; set; }
}

public class Input
{
    public string soid { get; set; }
    public string itemname { get; set; }
    public int qty { get; set; }
}

public class output
{
    public string soid { get; set; }
}

public class Messageformat
{
    public Attributecollection MyAttributecollection { get; set; }
    public Input MyInput { get; set; }
    public output Myoutput { get; set; }
}

...

Messageformat test = new Messageformat
    {
        MyAttributecollection = new Attributecollection { name = "", id = 1 },
        MyInput = new Input { soid = "", itemname ="", qty = 1 },
        Myoutput = new output { soid = "" }
    };

您应该将每个类单独定义。然后(WCF)您将能够创建您的DataContract及其指定的datamembers-属性,您可以在消息中发送什么:

  [DataContract]
  public class Messageformat
  {    
     [DataMember]
     public Attributecollection prop1;
     [DataMember]
     public Input prop2;
     [DataMember]
     public output prop3;
  } 

  [DataContract]
  public class Attributecollection
  {
     [DataMember]
     public string name { get; set; }
     [DataMember]
     public int id { get; set; }

  }

  [DataContract]
  public class Input
  {
      [DataMember]
      public string soid { get; set; }
      [DataMember]
      public string itemname { get; set; }
      [DataMember]
      public int qty { get; set; }
  }

  [DataContract]
  public class output
  {
      [DataMember]
      public string soid { get; set; }
  }
然后,您应该生成您的服务契约,在这里您可以使用您的类

  [ServiceContract]
  public interface IService
  {
     [OperationContract]
     SendMessage(Messageformat message);
  }

您应该将每个类单独定义。然后(WCF)您将能够创建您的DataContract及其指定的datamembers-属性,您可以在消息中发送什么:

  [DataContract]
  public class Messageformat
  {    
     [DataMember]
     public Attributecollection prop1;
     [DataMember]
     public Input prop2;
     [DataMember]
     public output prop3;
  } 

  [DataContract]
  public class Attributecollection
  {
     [DataMember]
     public string name { get; set; }
     [DataMember]
     public int id { get; set; }

  }

  [DataContract]
  public class Input
  {
      [DataMember]
      public string soid { get; set; }
      [DataMember]
      public string itemname { get; set; }
      [DataMember]
      public int qty { get; set; }
  }

  [DataContract]
  public class output
  {
      [DataMember]
      public string soid { get; set; }
  }
然后,您应该生成您的服务契约,在这里您可以使用您的类

  [ServiceContract]
  public interface IService
  {
     [OperationContract]
     SendMessage(Messageformat message);
  }

从外观/声音上看,您需要一个类的实例来公开其他类的属性:

public class Messageformat {

  public MessageFormat() {
    Attributes = new Attributecollection();
    Input = new Input();
    Output = new output();
  }

  public Attributecollection Attributes { get; set; }
  public Input Input { get; set; }
  public output Output { get; set; }
}

public class Attributecollection {
  public string name { get; set; }
  public int id { get; set; }
}

public class Input {
  public string soid { get; set; }
  public string itemname { get; set; }
  public int qty { get; set; }
}

public class output {
  public string soid { get; set; }
}
然后你可以做:

var format = new MessageFormat();
format.Input.soid = "something";
format.Input.itemname = "something";

等等。当然,这里有很多改进,但你已经做到了。

从外观/声音上看,你需要一个类的实例来公开其他类的属性:

public class Messageformat {

  public MessageFormat() {
    Attributes = new Attributecollection();
    Input = new Input();
    Output = new output();
  }

  public Attributecollection Attributes { get; set; }
  public Input Input { get; set; }
  public output Output { get; set; }
}

public class Attributecollection {
  public string name { get; set; }
  public int id { get; set; }
}

public class Input {
  public string soid { get; set; }
  public string itemname { get; set; }
  public int qty { get; set; }
}

public class output {
  public string soid { get; set; }
}
然后你可以做:

var format = new MessageFormat();
format.Input.soid = "something";
format.Input.itemname = "something";

等等。当然,这里有很多改进,但你已经做到了。

为什么不在访问级别中将内部类及其属性设置为静态的呢

 class MessageFormat
 {
    public static class Attributecollection
    {
        public static string name { get; set; }

        public static int id { get; set; }
    }

    public static class Input
    {
        public static string soid { get; set; }

        public static string itemname { get; set; }

        public static int qty { get; set; }
    }

    public static class output
    {
        public static string soid { get; set; }
    }
 }
然后您可以访问内部类,尽管不需要实例化:

  MessageFormat.Attributecollection.id = 1;
  MessageFormat.Attributecollection.name = "test";
  MessageFormat.Input.itemname = "Soda";
  MessageFormat.Input.qty = 10;

尽管它违背了嵌套类的目的,因为假定嵌套类的内部类只能由外部类或父类访问。但是根据您的要求,此代码将满足该要求。

为什么不在其访问级别中将内部类及其属性设置为静态的呢

 class MessageFormat
 {
    public static class Attributecollection
    {
        public static string name { get; set; }

        public static int id { get; set; }
    }

    public static class Input
    {
        public static string soid { get; set; }

        public static string itemname { get; set; }

        public static int qty { get; set; }
    }

    public static class output
    {
        public static string soid { get; set; }
    }
 }
然后您可以访问内部类,尽管不需要实例化:

  MessageFormat.Attributecollection.id = 1;
  MessageFormat.Attributecollection.name = "test";
  MessageFormat.Input.itemname = "Soda";
  MessageFormat.Input.qty = 10;

尽管它违背了嵌套类的目的,因为假定嵌套类的内部类只能由外部类或父类访问。但根据您的要求,此代码将满足该要求。

不确定,但这可以通过反射实现。你需要写下反射代码来填充你的类的子类不确定,但这可以通过反射来实现。你需要写下反射代码来填充你的类的子类,如果我写了怎么办如何填充它?如果我这样做了会怎么样如何填充它?如果我这样做了会怎么样如何填充它?如果我这样做了会怎么样如何填充它?