我有以下方法可以将一个对象序列化为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
}
}
有人能展示给我如何实现这个吗?
谢谢
new{}.GetType()
来说这是错误的。但我认为&& type.IsGenericType
可以安全地删除。 - Lars Corneliussen(type.Name.Contains("AnonymousType") || type.Name.Contains("AnonType"))
才能兼容Mono。来源: NancyFx扩展方法 - Justin Caldicott(type.Attributes & TypeAttributes.NotPublic) == TypeAttributes.NotPublic
这句话的意思是:检查类型的访问权限是否为非公开,但实际上TypeAttributes.NotPublic
的值为0,因此该检查总是为真。正确的检查应该使用TypeAttributes.Public
来判断类型是否为公开访问。 - user743382