这是我的代码:
bool ch=Type.IsBuiltIn("System.Int32"); // not working-> syntax error
public static class MyExtentions
{
public static bool IsBuiltIn(this Type t, string _type)
{
return (Type.GetType(_type) == null) ? false : true;
}
}
请我通过IsBuiltIn新方法扩展Type类
最佳答案
您不能有静态扩展方法。您的扩展方法适用于Type
类的实例,因此要调用它,您必须执行以下操作:
typeof(Type).IsBuiltIn("System.Int32")
解决方法是将扩展方法放在实用程序类中,例如类似于以下内容,并像普通的静态函数一样调用它:
public static class TypeExt
{
public static bool IsBuiltIn(string _type)
{
return Type.GetType(_type) == null;
}
}
// To call it:
TypeExt.IsBuiltIn("System.Int32")
顺便说一句,我认为这不会告诉您该类型是否是“内置”的。它只会告诉您是否已将具有给定名称的类型加载到流程中。
关于c# - 如何扩展类型类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18531218/