2015-07-21 131 views
0

以下代碼按原樣工作,但我想在構造函數 中使用MyProperty類的引用,而不是內聯代碼中的強類型引用。c#將類參數傳遞給構造函數

如何做到這一點,我預計到裁判傳遞給myProperty的,但一切我曾嘗試失敗

我想PropertyClass能夠處理任何myProperty的類即myProperty的任何引用在PropertyClass

如果我錯過了顯而易見的事物,仍然在學習如此抱歉!

任何幫助,非常感謝

薩拉

PropertyClass pc = new PropertyClass(!here!); // Would like to pass MyProperty class here 

pc.Settings.Add(new MyProperty("Fred", "Monday")); 
pc.SaveXml("MyTest.xml"); 




public class MyProperty 
{ 
    [XmlAttribute] 
    public string MyPropName { get; set; } 
    [XmlElement] 
    public string MyPropData { get; set; } 

    // default constructor needs to be parameterless for serialisation. 
    public MyProperty() 
    { 
    } 

    public MyProperty(string Name, string Data) 
    { 
     MyPropName = Name; 
     MyPropData = Data; 
    } 
} 



public class PropertyClass 
{ 
    public List<MyProperty> Settings { get; set; } 

    public PropertyClass()    // How to pass the required class here ? 
    {         // public PropertyClass(ref MyProperty myprop) 
     Settings = new List<MyProperty>(); 
    } 

    public void SaveXml(string fileName) 
    { 
     using (FileStream stream = new FileStream(fileName, FileMode.Create)) 
     { 
      XmlSerializer XML = new XmlSerializer(typeof(List<MyProperty>), new XmlRootAttribute("Settings")); 

      XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces(); 
      namespaces.Add(string.Empty, string.Empty); 

      XML.Serialize(stream, Settings, namespaces); 
     } 
    } 
} 
+0

你試圖通過只是你要存儲的對象類型「PropertyClass」的實例?你有沒有嘗試過使用泛型?或者是泛型的序列化問題? –

+1

我想你正在尋找[泛型](https://msdn.microsoft.com/en-us/library/512aeb7t.aspx)?... –

+0

嗨很多感謝回覆 - 我希望有一個密封的類,可以處理傳遞給它的任何'MyProperty'樣式類。 MyProperty類可以包含任意數量的對象,並且提供的類的序列化是核心部分。總之,我只是不希望有一個PropertyClass的所有MyProperty要求,我也需要完全理解爲什麼我試圖做的不工作! –

回答

2

我的PropertyClass的定義修改爲

public class PropertyClass<T> 
{ 
    public List<T> Settings { get; set; } 

    public PropertyClass() 
    { 
     Settings = new List<T>(); 
    } 

    public void SaveXml(string fileName) 
    { 
     using (FileStream stream = new FileStream(fileName, FileMode.Create)) 
     { 
      XmlSerializer XML = new XmlSerializer(typeof(List<T>), new XmlRootAttribute("Settings")); 

      XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces(); 
      namespaces.Add(string.Empty, string.Empty); 

      XML.Serialize(stream, Settings, namespaces); 
     } 
    } 
} 

type parameterT指定項目的類型在List<T>中,以便您可以實例化PropertyClass,如下所示

var pc = new PropertyClass<MyProperty>(); 

或者當您厭倦了MyProperty時,您可以將其更改爲new PropertyClass<foo>(),而無需在其他地方更改它。

另一個不錯的功能,我喜歡泛型是,你實際上可以把限制的類型參數的線,你聲明它想:

public class PropertyClass<T> where T : MyClass, IMyInterface, new() 

這意味着T必須從MyClass衍生,它必須實現IMyInterface並且必須有一個無參數的構造函數。(顯然你不需要添加所有這樣的約束,但是在某些情況下它們都可以有用)。

我想多說一點,但我相信你可以玩它並找到它的一些用途。

+0

哇謝謝,這很快,我需要一點時間來吸收你說的話,但我想我明白你說的話(驚訝自己!),我真的不喜歡做我不懂的事情,我喜歡做5年後可以理解的整潔代碼。順便說一句,我正在從Forth轉移到C#,所以沒有任何意義上的壯舉。非常感謝您的幫助。 –

+0

Namelesss一個感謝泛型實現了我所需要的,我已經標記了你的答案爲接受。很多謝謝你的解釋。 –

0

你這是怎麼調用構造函數

PropertyClass pc = new PropertyClass(new MyProperty("Fred", "Monday")); 

這是構造

Public MyProperty MyProperty { get; set; } 
public PropertyClass(MyProperty _myProperty) 
{ 
    MyProperty = _myProperty 
} 
+1

你應該通過添加解釋性的細節來改善這個答案的質量。所以對代碼唯一的答案皺眉。 –

+0

@WilliamCustode它是代碼,只是插入問題 – Paparazzi

0

您需要添加建築物uctor採用一個MyProperty作爲參數:

public PropertyClass(MyProperty myprop)    
{         
    Settings = new List<MyProperty> {myprop}; 
} 

注意MyProperty是引用類型所以ref是不必要這裏(它已經的引用)。

+0

我已經嘗試過,但如何劑量傳遞類的序列化拿起其參考。我覺得我在這裏錯過了一些重要的概念。我需要這個類能夠處理許多不同的'MyProperty'類,它們將具有不同的配置。因此,如果我使用上面的方法,serialize類如何使用上述概念? –

1

我認爲你需要一個通用的類。

public class PropertyClass<T> 
{ 
    public List<T> Settings { get; set; } 

    public PropertyClass() 
    { 
    Settings = new List<T>(); 
    } 
... 
} 

PropertyClass<MyProperty> pc = new PropertyClass<MyProperty>(); 

我必須補充說明你的命名很不清楚。 PropertyClass應該叫做XmlableList。而MyProperty已經存在,被稱爲NameValuePair<string,string>

+0

對不起,我試圖讓我做的事很簡單,顯然失敗了,但我已經聽取了你所說的話。很多謝謝 –

1

也許你正在尋找仿製藥:

public class PropertyClass<TMyProperty> 
{ 
    public List<TMyProperty> Settings { get; set; } 

    public PropertyClass() 
    {         
     Settings = new List<TMyProperty>(); 
    } 
    .. 
} 
1

處理您的命名,PropertyClass實際上是一組屬性;也許MyPropertyCollection會更好?

你在找什麼叫做構造函數重載。基本上你再次指定構造,但這次參數:

public MyPropertyCollection() 
{ 
    Settings = new List<MyProperty>(); 
} 
public MyPropertyCollection(IEnumerable<MyProperty> collection) 
{ 
    Settings = new List<MyProperty>(collection); 
} 

或者允許var col = new MyPropertyCollection(new MyProperty(), new MyProperty(), new MyProperty())你可以這樣做:

public MyPropertyCollection(params MyProperty[] collection) 
{ 
    Settings = new List<MyProperty>(collection); 
} 

雖然你應該小心的是,它並不感到右如果您稍後想要引入其他參數,那麼它可能會失敗。

而且,你基本上包裹的列表,你也可以考慮的是System.Collection.ObjectModel.Collection<T>類爲基礎:

// The Collection<MyProperty> base class is responsible for maintaining the list 
public class MyPropertyCollection : Collection<MyProperty> 
{ 
    public MyPropertyCollection() 
    { 
     // Default base() constructor is called automatically 
    } 

    public MyPropertyCollection(IList<MyProperty> properties) 
     : base(properties) 
    { 
     // Overloaded constructor calls base constructor with collection of properties 
    } 

    public void SaveXml(string fileName) 
    { 
     using (var stream = new FileStream(fileName, FileMode.Create)) 
     { 
      // Serializer should now target this very class 
      var xml = new XmlSerializer(typeof (this), new XmlRootAttribute("Settings")); 

      var namespaces = new XmlSerializerNamespaces(); 
      namespaces.Add(string.Empty, string.Empty); 

      xml.Serialize(stream, this, namespaces); 
     } 
    } 
} 
+0

對不起,它被稱爲MypropCollection,但由於我沒有使用集合,我改變了它的名字來問這個問題。我真誠地感謝你的回覆,現在需要消化你所說的話,我需要理解我爲什麼和我正在做什麼,並且你可以猜測這是一個更大的項目的一小部分,之前的編碼已經全部出現了所有這些都有所不同。讚賞莎拉 –