我有以下方法将对象序列化为HTML标记。我只想做这个,但是如果这个类型不是匿名的。如何测试类型是匿名的?
private void MergeTypeDataToTag(object typeData)
{
if (typeData != null)
{
Type elementType = typeData.GetType();
if (/* elementType != AnonymousType */)
{
_tag.Attributes.Add("class", elementType.Name);
}
// do some more stuff
}
}
有人可以告诉我如何做到这一点?
感谢
来自http://jclaes.blogspot.com/2011/05/checking-for-anonymous-types.html – serhio 2011-09-06 12:28:18
不幸的是这是'new {}的错误。GetType()' 但我想'&&类型。 IsGenericType'可以保存被删除。 – 2011-09-14 16:15:59
看起来像你需要'(type.Name.Contains(“AnonymousType”)|| type.Name.Contains(“AnonType”))'与Mono兼容。来源:[NancyFx扩展方法](https://github.com/NancyFx/Nancy/blob/master/src/Nancy/ViewEngines/Extensions.cs) – 2015-07-24 10:33:44