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

c#将类参数传递给构造函数

c#将类参数传递给构造函数,c#,object,constructor,C#,Object,Constructor,以下代码按原样工作,但我希望使用对要在构造函数中传递的MyProperty类的引用 而不是内联代码中的强类型引用 我该怎么做呢?我希望传递一个对MyProperty的引用,但我尝试过的一切都失败了 我希望PropertyClass能够处理任何MyProperty类,即PropertyClass中没有对MyProperty的引用 仍然在学习,如果我错过了显而易见的东西,我很抱歉 非常感谢你的帮助 莎拉 PropertyClass pc=newpropertyclass(!here!);//想在这里

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

我该怎么做呢?我希望传递一个对MyProperty的引用,但我尝试过的一切都失败了

我希望PropertyClass能够处理任何MyProperty类,即PropertyClass中没有对MyProperty的引用

仍然在学习,如果我错过了显而易见的东西,我很抱歉

非常感谢你的帮助

莎拉

PropertyClass pc=newpropertyclass(!here!);//想在这里传递MyProperty类吗
pc.Settings.Add(新的MyProperty(“Fred”、“星期一”);
pc.SaveXml(“MyTest.xml”);
公共类MyProperty
{
[XmlAttribute]
公共字符串MyPropName{get;set;}
[XmlElement]
公共字符串MyPropData{get;set;}
//对于序列化,默认构造函数需要无参数。
公共财产()
{
}
公共MyProperty(字符串名称、字符串数据)
{
MyPropName=名称;
MyPropData=数据;
}
}
公共类PropertyClass
{
公共列表设置{get;set;}
public PropertyClass()//如何在此处传递所需的类?
{//publicpropertyclass(ref MyProperty myprop)
设置=新列表();
}
公共void SaveXml(字符串文件名)
{
使用(FileStream-stream=newfilestream(fileName,FileMode.Create))
{
XmlSerializer XML=新的XmlSerializer(typeof(List),新的XmlRootAttribute(“设置”);
XmlSerializerNamespaces=新的XmlSerializerNamespaces();
添加(string.Empty,string.Empty);
序列化(流、设置、名称空间);
}
}
}

这是如何调用构造函数的

PropertyClass pc = new PropertyClass(new MyProperty("Fred", "Monday"));  
Public MyProperty MyProperty { get; set; }
public PropertyClass(MyProperty _myProperty) 
{
    MyProperty  = _myProperty 
}
这是构造函数

PropertyClass pc = new PropertyClass(new MyProperty("Fred", "Monday"));  
Public MyProperty MyProperty { get; set; }
public PropertyClass(MyProperty _myProperty) 
{
    MyProperty  = _myProperty 
}

这就是如何调用构造函数

PropertyClass pc = new PropertyClass(new MyProperty("Fred", "Monday"));  
Public MyProperty MyProperty { get; set; }
public PropertyClass(MyProperty _myProperty) 
{
    MyProperty  = _myProperty 
}
这是构造函数

PropertyClass pc = new PropertyClass(new MyProperty("Fred", "Monday"));  
Public MyProperty MyProperty { get; set; }
public PropertyClass(MyProperty _myProperty) 
{
    MyProperty  = _myProperty 
}

您需要添加一个将
MyProperty
作为参数的构造函数:

public PropertyClass(MyProperty myprop)             
{                                   
    Settings = new List<MyProperty> {myprop};
}
公共属性类(MyProperty myprop)
{                                   
设置=新列表{myprop};
}

请注意,
MyProperty
是一个引用类型,因此这里不需要
ref
(它已经是一个引用)。

您需要添加一个将
MyProperty
作为参数的构造函数:

public PropertyClass(MyProperty myprop)             
{                                   
    Settings = new List<MyProperty> {myprop};
}
公共属性类(MyProperty myprop)
{                                   
设置=新列表{myprop};
}

请注意,
MyProperty
是一个引用类型,因此这里不需要
ref
(它已经是一个引用)。

我认为您需要一个泛型类

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

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

PropertyClass<MyProperty> pc = new PropertyClass<MyProperty>();
公共类PropertyClass
{
公共列表设置{get;set;}
公共财产类别()
{
设置=新列表();
}
...
}
PropertyClass pc=新的PropertyClass();

我必须补充一点,你的名字很不清楚
PropertyClass
应该被称为类似于
XmlableList
。而
MyProperty
已经存在,名为
NameValuePair
我想您需要一个泛型类

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

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

PropertyClass<MyProperty> pc = new PropertyClass<MyProperty>();
公共类PropertyClass
{
公共列表设置{get;set;}
公共财产类别()
{
设置=新列表();
}
...
}
PropertyClass pc=新的PropertyClass();

我必须补充一点,你的名字很不清楚
PropertyClass
应该被称为类似于
XmlableList
。和
MyProperty
已经存在,并被称为
NameValuePair

可能您正在寻找泛型:

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

    public PropertyClass()
    {                                   
        Settings = new List<TMyProperty>();
    }
    ..
 }
公共类PropertyClass
{
公共列表设置{get;set;}
公共财产类别()
{                                   
设置=新列表();
}
..
}

您可能正在寻找泛型:

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

    public PropertyClass()
    {                                   
        Settings = new List<TMyProperty>();
    }
    ..
 }
公共类PropertyClass
{
公共列表设置{get;set;}
公共财产类别()
{                                   
设置=新列表();
}
..
}

我会将
属性类的定义更改为

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);
        }
    }
}
或者当您厌倦了
MyProperty
时,您可以将其更改为
newpropertyclass()
,而无需在其他地方更改它

关于泛型,我喜欢的另一个很好的特性是,实际上可以在声明类型参数的行中放置约束,如下所示:

public class PropertyClass<T> where T : MyClass, IMyInterface, new()
public-class-PropertyClass其中T:MyClass,IMyInterface,new()
这意味着
T
必须从
MyClass
派生,它必须实现
IMyInterface
,并且必须具有无参数构造函数。(显然,您不需要添加所有此类约束,但它们在某些情况下都很有用)


我想多说一点,但我相信你可以玩它,并找到它的一些用途。

我会将
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);
        }
    }
}
或者当您厌倦了
MyProperty
时,您可以将其更改为
newpropertyclass()
,而无需在其他地方更改它

关于泛型,我喜欢的另一个很好的特性是,实际上可以在声明类型参数的行中放置约束,如下所示:

public class PropertyClass<T> where T : MyClass, IMyInterface, new()
public-class-PropertyClass其中T:MyClass,IMyInterface,new()
这意味着
T
必须从
MyClass
派生,它必须实现
IMyInterface
,并且必须具有无参数构造函数。(显然,您不需要添加所有此类约束,但它们在某些情况下都很有用)


我想再多说一点,但我相信你可以玩它,并找到它的一些用途。

在你的命名上,不太明显PropertyClass实际上是一个属性集合;也许MyPropertyCollection会更好

您正在寻找的是所谓的构造函数重载。基本上,您可以指定构造