泛型类型T,其中T具有特定属性的 [英] Generic of type T where T has a specific attribute
本文介绍了泛型类型T,其中T具有特定属性的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有可能创建类型的一般方法 T
,其中 T
具有特定属性?
例如:
公共静态的XmlDocument SerializeObjectToXml< T>(T OBJ)
{
// ...
}
和我想用序列只有一个班序列化
和/或 DataContract
属性:
[Serializable接口]
[DataContract(NAME =视图模型,命名空间=NS)]
内部类视图模型
{
// ...
}
解决方案
恐怕没有。有3种类型的约束:推导,构造和参考/价值型。
我相信,你应该检查在方法体的属性,如果序列化对象不符合标准调用不同的方法来处理它。
Is it possible to create a generic method of type T
where T
has a specific attribute?
E.g.:
public static XmlDocument SerializeObjectToXml<T>(T obj)
{
//...
}
and I want to serialize only a classes with a Serializable
and/or DataContract
attribute:
[Serializable]
[DataContract(Name = "viewModel", Namespace = "ns")]
internal class ViewModel
{
//...
}
解决方案
I'm afraid no. There are 3 types of constraints: derivation, constructor and reference/value-type.
I believe, you should check for attributes in the method body and if the serializable object doesn't meet the criteria call a different method to process it.
这篇关于泛型类型T,其中T具有特定属性的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文