2013-02-25 28 views
4

我有兩個類:生成空的對象 - 與空性的空類/子類C#

public class HumanProperties { int prop1; int prop2; string name;} 

public class Human{int age; HumanProperties properties;} 

現在,如果我要創造人類的新實例,我必須做Human person = new Human(); 但是,當我試圖訪問像person.properties.prop1=1;然後我有nullRefrence屬性,因爲我必須做出新的屬性。 我必須做出這樣的:

Human person = new Human(); 
person.properties = new HumanProperties(); 

,現在我可以訪問此person.properties.prop1=1;

這是小的例子,但我從XSD產生巨大的階級和我沒有那麼多時間生成手動此「人」類及其所有子類。 有沒有什麼辦法如何以編程方式做到這一點,或有一些發電機?

或者我可以通過類循環,併爲每個屬性新的類typeof屬性,並將其加入到父類?

謝謝!

+0

值得注意的是你的類沒有屬性,它只是公共領域。他們可能*應該*屬性,而不是目前。 – Servy 2013-02-25 07:22:21

回答

7

我沒有的東西有一種傳統的方式做你問什麼,作爲類的默認類型是null。但是,您可以使用反射以遞歸方式循環訪問屬性,查找具有無參數構造函數的公共屬性並初始化它們。像這樣的東西應該工作(未經測試):

void InitProperties(object obj) 
{ 
    foreach (var prop in obj.GetType() 
     .GetProperties(BindingFlags.Public | BindingFlags.Instance) 
     .Where(p => p.CanWrite)) 
    { 
     var type = prop.PropertyType; 
     var constr = type.GetConstructor(Type.EmptyTypes); //find paramless const 
     if (type.IsClass && constr != null) 
     { 
      var propInst = Activator.CreateInstance(type); 
      prop.SetValue(obj, propInst, null); 
      InitProperties(propInst); 
     } 
    } 
} 

然後你就可以使用這個像這樣:

var human = new Human(); 
InitProperties(human); 
+0

謝謝!那正是我需要的。 – Muno 2013-02-25 07:58:45

1

你可以改變你的類聲明如下:

public class Human 
{ 
    int age; 
    HumanProperties properties = new HumanProperties(); 
} 
3

我會建議你使用構造函數:

public class Human 
{ 
    public Human() 
    { 
    Properties = new HumanProperties(); 
    } 

    public int Age {get; set;} 
    public HumanProperties Properties {get; set;} 
} 
+0

這是問題中示例最明顯的解決方案。但是,我認爲OP正在要求初始化嵌套類屬性的深層次結構,而不僅僅是一個級別。想象一下'HumanProperties'具有'InnerProperties',而那些具有'DetailedInnerProperties'等等。如果我正確地理解了這個問題,那麼OP想要將所有那些初始化爲「不爲空」,而無需手動爲每個創建一個「新的Instance()他們在構造函數中。 – 2013-02-25 07:37:02

+1

在我看來 - 每個類都負責實例化自己的屬性。我相信最好在該類的構造函數中完成。無論類層次結構還是嵌套屬性。 – 2013-02-25 07:43:44

+0

我同意你的意見。然而,如果這個類是爲一般用途而設計的,你可能想也可能不想初始化這些屬性 - 也許最好把它們留作'null'。如果這個用戶有一個他想以這種方式初始化的一次性用例,我不會通過更改構造函數來向所有用戶指定。 – 2013-02-25 07:59:28

0

.NET利用的特性。

您可以使用Visual Studio鍵盤快捷鍵:Ctrl + r,Ctrl + e自動生成屬性。

試試這個:

public class HumanProperties 
{ 
    public int Prop1 
    { 
     get { return _prop1; } 
     set { _prop1 = value; } 
    } 
    private int _prop1 = 0; 

    public int Prop2 
    { 
     get { return _prop2; } 
     set { _prop2 = value; } 
    } 
    private int _prop2; 

    public string Name 
    { 
     get { return _name; } 
     set { _name = value; } 
    } 
    private string _name = String.Empty; 
} 

public class Human 
{ 
    public int Age 
    { 
     get { return _age; } 
     set { _age = value; } 
    } 
    private int _age = 0; 

    public HumanProperties Properties 
    { 
     get { return _properties; } 
     set { _properties = value; } 
    } 
    private HumanProperties _properties = new HumanProperties(); 
} 
+0

爲什麼打擾手動創建屬性,如果你只能使用自動屬性? – Servy 2013-02-25 07:21:09

+0

@Servy如果您手動創建它們,則可以指定默認值。在這種情況下,您可以確保HumanProperties _properties不爲null。 – rhughes 2013-02-25 07:29:23

+0

您可以在構造函數中爲代碼中的自動屬性執行哪項操作。 – Servy 2013-02-25 14:37:22