C#将类参数传递给构造函数 [英] c# passing class argument to constructor

查看:58
本文介绍了C#将类参数传递给构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码按原样工作,但我想使用对MyProperty类的引用以在构造函数中传递而不是内联代码中的强类型引用.

The following codes works as is, but I would like to use a reference to the MyProperty class to be passed in the constructor instead of the strongly typed references in the inline code.

我该怎么做,我希望将ref传递给MyProperty,但是我尝试的所有方法都失败了

How do I do this, I expected to pass a ref to MyProperty but everything I have tried fails

我希望PropertyClass能够处理任何MyProperty类,即在PropertyClass中不引用MyProperty

I would like PropertyClass to be able to handle any MyProperty classes i.e. no references to MyProperty in PropertyClass

如果我错过了明显的事情,仍然很抱歉!

Still learning so sorry if I have missed the obvious !

非常感谢您的帮助

萨拉

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

推荐答案

我将 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);
        }
    }
}

类型参数 T 指定 List< T> 中项目的类型,以便您可以实例化 PropertyClass 如下

The type parameter T specifies the type of the items in the List<T>, so that you can instantiate PropertyClass as follows

var pc = new PropertyClass<MyProperty>();

或者当您厌倦了 MyProperty 时,可以将其更改为 new PropertyClass< foo>(),而无需在其他位置进行更改.

Or when you get tired of MyProperty you can change it to new PropertyClass<foo>() without changing it elsewhere.

我喜欢泛型的另一个不错的功能是,您实际上可以在声明它的行中的类型参数上放置约束,例如:

Another nice feature that I like about generics is that you can actually place constraints on the type parameter in the line where you declare it like:

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

这意味着 T 必须从 MyClass 派生,它必须实现 IMyInterface ,并且必须具有无参数的构造函数.(显然,您无需添加所有此类约束,但是在某些情况下它们都可能有用).

This means that T has to be derived from MyClass, it has to implement IMyInterface and has to have a parameterless constructor. (Obviously you do not need to add all such constraints, but they can all be useful in certain cases).

我想大声一点,但是我敢肯定,您可以使用它并找到一些用途.

I want to rant a little more, but I am sure you can play with it and find some uses for it.

这篇关于C#将类参数传递给构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆