2013-04-17 70 views
1

我有一個類如下所示。我想發送一個Messageformat這樣的一個web服務,所以我需要將值添加到AttributecollectionInput如何在另一個類中的類中添加數據

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並通過該對象向子類中添加值。我知道它是一個嵌套的類結構。我不是很好,所以任何人都可以分享一些想法嗎?

+0

不知道,但**的事情可以通過** Reflection **來實現。您需要寫下反射代碼以填充班級的子類[使用反射填充數據對象](http://www.c-sharpcorner.com/uploadfile/sridhar_subra/filling-data-objects-using-reflection/) –

回答

1

你應該讓每個類別分開定義。然後(WCF),你就可以創建你的DataContract及其指定其數據成員 - 屬性,你怎麼在您的短信發送:

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

你爲什麼這樣做?相反,嵌套的類,在同級別聲明你的類並創建其他類的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的例子,但你可以做任何你想要的)

+0

如果我做了'List lmf = new List ();'我如何填充它? – Si8

1

您的問題沒有說清楚,但如果你問如何填充數據,你就必須添加什麼所謂性能到你的父母班。請注意,您不必使用內部類。

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 = "" } 
    }; 
1

從外型/它的聲音,你希望那公開他人財產一個類的實例:

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"; 

依此類推。在那裏,可以肯定的是,在這裏有很多改進,但是在那裏你可以擁有它。

+0

如果我做了'List lmf = new List ();'我如何填充它? – Si8

1

爲什麼不把內部類和它的屬性靜態在其訪問級別是這樣的:

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; 

雖然它違背了嵌套類的目的,因爲據推測嵌套類的內部類只能由外部類或父類訪問。但根據您的要求,此代碼將符合該要求。

相關問題