Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.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
限制MyClass转换错误的c#扩展方法_C#_Type Conversion_Extension Methods - Fatal编程技术网

限制MyClass转换错误的c#扩展方法

限制MyClass转换错误的c#扩展方法,c#,type-conversion,extension-methods,C#,Type Conversion,Extension Methods,我有一个对MyClass有限制的扩展方法。我想做的是: public static void GetXmlDocValues<T>(this List<T> collection, XmlDocument xmlDoc) where T : MyClass { collection.Clear(); foreach (XmlNode item in xmlDoc.ChildNodes) { ((List<MyClass

我有一个对MyClass有限制的扩展方法。我想做的是:

public static void GetXmlDocValues<T>(this List<T> collection, XmlDocument xmlDoc) where T : MyClass
{
     collection.Clear();
     foreach (XmlNode item in xmlDoc.ChildNodes)
     {
         ((List<MyClass>)collection).Add(new MyClass(item));
     }

}
publicstaticvoid GetXmlDocValues(这个列表集合,xmldocumentxmldoc),其中T:MyClass
{
collection.Clear();
foreach(xmlDoc.ChildNodes中的XmlNode项)
{
((列表)集合)。添加(新MyClass(项目));
}
}
然后我得到了一个错误:

无法将类型“
System.Collections.Generic.List
”转换为 “
System.Collections.Generic.List
”。 此处:
((列表)集合)


有可能做那件事吗?或者它在任何情况下都会出错。

看起来您确实希望您的方法如下所示:

public static void GetXmlDocValues(this List<MyClass> collection, XmlDocument xmlDoc)
{
    collection.Clear();
    foreach (XmlNode item in xmlDoc.ChildNodes)
    {
        collection.Add(new MyClass(item));
    }
}
publicstaticvoid GetXmlDocValues(此列表集合,XmlDocument xmlDoc)
{
collection.Clear();
foreach(xmlDoc.ChildNodes中的XmlNode项)
{
集合。添加(新MyClass(项目));
}
}

如果您只想使用
MyClass

是的,那么不需要通用方法。为什么我们需要“哪里”限制?仅用于覆盖?@Novikov:
其中
来自泛型的使用。没有泛型,不需要
where
。一个
Get
方法返回
void
!!!并修改其对象!!!并删除!!!如何命名它
重新加载
?(“从XML”将由参数类型暗示。)