C# 检查给定字符串是否内置于数据类型名称中,数据类型名称是系统命名空间中预定义类型的别名

C# 检查给定字符串是否内置于数据类型名称中,数据类型名称是系统命名空间中预定义类型的别名,c#,asp.net,.net,ado.net,C#,Asp.net,.net,Ado.net,如何查找给定字符串是否为内置类型,这些类型是系统命名空间中预定义类型的别名。 例:1 例:2 Input = "xxx" Output = False(indicating it's not Built-In Type name) 对此,我们非常感谢您的任何建议 提前感谢假设您想检查类型名称是否属于sql类型。框架中没有可用的属性或方法 但是,如果未明确设置,则可以执行从Type到SqlDbType的SqlParameter操作 以下方法直接派生自SqlParameter的推断SQLType:

如何查找给定字符串是否为内置类型,这些类型是系统命名空间中预定义类型的别名。
例:1

例:2

Input = "xxx"
Output = False(indicating it's not Built-In Type name)
对此,我们非常感谢您的任何建议


提前感谢

假设您想检查类型名称是否属于sql类型。框架中没有可用的属性或方法

但是,如果未明确设置,则可以执行从
Type
SqlDbType
SqlParameter
操作

以下方法直接派生自SqlParameter的
推断SQLType

public static bool IsConvertibleToSqlDbType(String type)
{
    switch(type) {
        case "System.Int64":
        case "System.Data.SqlTypes.SqlInt64":
            return true;
        case "System.Boolean":
        case "System.Data.SqlTypes.SqlBoolean":
            return true;
        case "System.String":
        case "System.Data.SqlTypes.SqlString":
            return true;
        case "System.DateTime":
        case "System.Data.SqlTypes.SqlDateTime":
            return true;
        case "System.Decimal":
        case "System.Data.SqlTypes.SqlDecimal":
            return true;
        case "System.Double":
        case "System.Data.SqlTypes.SqlDouble":
            return true;
        case "System.Byte[]":
        case "System.Data.SqlTypes.SqlBinary":
            return true;
        case "System.Byte":
        case "System.Data.SqlTypes.SqlByte":
            return true;
        case "System.Int32":
        case "System.Data.SqlTypes.SqlInt32":
            return true;
        case "System.Single":
        case "System.Data.SqlTypes.Single":
            return true;
        case "System.Int16":
        case "System.Data.SqlTypes.SqlInt16":
            return true;
        case "System.Guid":
        case "System.Data.SqlTypes.SqlGuid":
            return true;
        case "System.Money":
        case "System.SmallMoney":
        case "System.Data.SqlTypes.SqlMoney":
            return true;
        case "System.Object":
            return true;
        default:
            return false;
    }
}

假设要检查类型名称是否属于sql类型。框架中没有可用的属性或方法

但是,如果未明确设置,则可以执行从
Type
SqlDbType
SqlParameter
操作

以下方法直接派生自SqlParameter的
推断SQLType

public static bool IsConvertibleToSqlDbType(String type)
{
    switch(type) {
        case "System.Int64":
        case "System.Data.SqlTypes.SqlInt64":
            return true;
        case "System.Boolean":
        case "System.Data.SqlTypes.SqlBoolean":
            return true;
        case "System.String":
        case "System.Data.SqlTypes.SqlString":
            return true;
        case "System.DateTime":
        case "System.Data.SqlTypes.SqlDateTime":
            return true;
        case "System.Decimal":
        case "System.Data.SqlTypes.SqlDecimal":
            return true;
        case "System.Double":
        case "System.Data.SqlTypes.SqlDouble":
            return true;
        case "System.Byte[]":
        case "System.Data.SqlTypes.SqlBinary":
            return true;
        case "System.Byte":
        case "System.Data.SqlTypes.SqlByte":
            return true;
        case "System.Int32":
        case "System.Data.SqlTypes.SqlInt32":
            return true;
        case "System.Single":
        case "System.Data.SqlTypes.Single":
            return true;
        case "System.Int16":
        case "System.Data.SqlTypes.SqlInt16":
            return true;
        case "System.Guid":
        case "System.Data.SqlTypes.SqlGuid":
            return true;
        case "System.Money":
        case "System.SmallMoney":
        case "System.Data.SqlTypes.SqlMoney":
            return true;
        case "System.Object":
            return true;
        default:
            return false;
    }
}

我想您正在寻找Assembly.GetType(字符串)

如果该方法返回null,则程序集不知道类型

然后您只需要知道要检查哪个程序集

在您的情况下,看起来系统组件就足够了


Jeppe提出了一个很好的观点,请参见下面的评论。

我认为您正在寻找Assembly.GetType(string)

public bool IsBuiltInType(string typeName)
{
  return Type.GetType(typeName, false) != null; 
}
如果该方法返回null,则程序集不知道类型

然后您只需要知道要检查哪个程序集

在您的情况下,看起来系统组件就足够了

杰普提出了一个很好的观点,见下面他的评论

public bool IsBuiltInType(string typeName)
{
  return Type.GetType(typeName, false) != null; 
}
我想这就是你想要的

上面的返回值为true,因为它找到System.Int32类型-如果找不到指定的类型,也可以抛出错误

我想这就是你想要的

上面的返回值为true,因为它找到了System.Int32类型-如果找不到指定的类型,也可以抛出错误。

type.GetType(“System.Int32”,false)!=null
会让您知道该类型是否存在。但是,它也会检查当前应用程序域中加载的类型。

Type.GetType(“System.Int32”,false)!=null
会让您知道该类型是否存在。但是,它也会检查当前应用程序域中加载的类型。

这样如何:

static class TypeHelper
{
  public static readonly IList<string> BuiltInTypeNames;

  static TypeHelper()
  {
    Type[] allBuiltInTypes = { typeof(string), typeof(int), typeof(decimal), }; // add whatever types you consider "built-in".
    BuiltInTypeNames = allBuiltInTypes.Select(t => t.FullName).ToList().AsReadOnly();
  }
}
静态类TypeHelper
{
公共静态只读IList内置程序;
静态类型助手()
{
类型[]AluBuyTyType = {Type(String)、Type of(int)、Type of(十进制)、};//添加任何你认为“内置”的类型。
BuiltInTypeNames=allBuiltInTypes.Select(t=>t.FullName).ToList().AsReadOnly();
}
}
然后,使用输入字符串
input
可以说
TypeHelper.builtentypenames.Contains(input)
来获得所需的布尔值(真/假值)。

这样如何:

static class TypeHelper
{
  public static readonly IList<string> BuiltInTypeNames;

  static TypeHelper()
  {
    Type[] allBuiltInTypes = { typeof(string), typeof(int), typeof(decimal), }; // add whatever types you consider "built-in".
    BuiltInTypeNames = allBuiltInTypes.Select(t => t.FullName).ToList().AsReadOnly();
  }
}
静态类TypeHelper
{
公共静态只读IList内置程序;
静态类型助手()
{
类型[]AluBuyTyType = {Type(String)、Type of(int)、Type of(十进制)、};//添加任何你认为“内置”的类型。
BuiltInTypeNames=allBuiltInTypes.Select(t=>t.FullName).ToList().AsReadOnly();
}
}

然后使用输入字符串
input
可以说
TypeHelper.builtentypenames.Contains(input)
来获得所需的布尔值(真/假值)。

您如何定义“内置”呢?是否有任何框架类可以计算,或者您是否将其限制为“那些在mscorlib中定义的”或类似的内容?
ado.net
与这个问题有什么关系?请更好地解释您的问题和您的要求。@RB:对不起,我将内置类型称为“c#预定义类型”@cHao我想这个问题的意思是内置.NET值类型,例如
string
int
double
bool
,但它们的类型名与类型相对应themselves@sandeep:C#“预定义类型”只是实际类型的别名。框架没有办法(在C#编译器名称空间之外)要知道某个类型在某些特定语言中是否有别名,您很可能需要手动检查。@Luke:我有点明白了,但我自己事后猜测并删除了注释。结果发现问题大约是我认为的90%:)您将“内置”定义为什么"? 是否有任何框架类可以计算,或者您是否将其限制为“那些在mscorlib中定义的”或类似的内容?
ado.net
与这个问题有什么关系?请更好地解释您的问题和您的要求。@RB:对不起,我将内置类型称为“c#预定义类型”@cHao我想这个问题的意思是内置.NET值类型,例如
string
int
double
bool
,但它们的类型名与类型相对应themselves@sandeep:C#“预定义类型”只是实际类型的别名。框架没有办法(在C#编译器名称空间之外)要知道某个类型在某些特定语言中是否有别名,您很可能需要手动检查。@Luke:我有点明白了,但后来我自己猜测并删除了注释。结果发现问题大约是我认为的90%:)对于当前加载的其他类型,包括user define,这也会返回trued类。例如
“MyProjectName.MySubNamespace.SomeClassByImy”
可能返回
true
。对于当前加载的其他类型,包括用户定义的类,这也会返回true。例如
“MyProjectName.MySubNamespace.SomeClassByImy”“
可能返回
true
。他可以使用
System.dll
程序集,但我认为他需要
mscorlib.dll
程序集。这是定义基本类型的地方。他可以得到这方面的参考