本帖最后由 jyswjjgdwtdtj 于 2024-9-22 21:53 编辑
不用猜了- public static VariantType VarType(object VarName) {
- if (VarName == null) {
- return VariantType.Object;
- }
- return VarTypeFromComType(VarName.GetType());
- }
- internal static VariantType VarTypeFromComType(Type typ) {
- if ((object) typ == null) {
- return VariantType.Object;
- }
-
- if (typ.IsArray) {
- typ = typ.GetElementType();
- if (typ.IsArray) {
- return (VariantType) 8201;
- }
-
- VariantType variantType = VarTypeFromComType(typ);
- if ((variantType & VariantType.Array) != 0) {
- return (VariantType) 8201;
- }
-
- return variantType | VariantType.Array;
- }
-
- if (typ.IsEnum) {
- typ = Enum.GetUnderlyingType(typ);
- }
-
- if ((object) typ == null) {
- return VariantType.Empty;
- }
-
- switch (Type.GetTypeCode(typ)) {
- case TypeCode.String:
- return VariantType.String;
- case TypeCode.Int32:
- return VariantType.Integer;
- case TypeCode.Int16:
- return VariantType.Short;
- case TypeCode.Int64:
- return VariantType.Long;
- case TypeCode.Single:
- return VariantType.Single;
- case TypeCode.Double:
- return VariantType.Double;
- case TypeCode.DateTime:
- return VariantType.Date;
- case TypeCode.Boolean:
- return VariantType.Boolean;
- case TypeCode.Decimal:
- return VariantType.Decimal;
- case TypeCode.Byte:
- return VariantType.Byte;
- case TypeCode.Char:
- return VariantType.Char;
- case TypeCode.DBNull:
- return VariantType.Null;
- default:
- if ((object) typ == typeof(Missing) || (object) typ == typeof(Exception) || typ.IsSubclassOf(typeof(Exception))) {
- return VariantType.Error;
- }
-
- if (typ.IsValueType) {
- return VariantType.UserDefinedType;
- }
-
- return VariantType.Object;
- }
- }
- public enum VariantType {
- Empty = 0, Null = 1, Short = 2, Integer = 3, Single = 4, Double = 5, Currency = 6, Date = 7, String = 8, Object = 9, Error = 10, Boolean = 11, Variant = 12, DataObject = 13, Decimal = 14, Byte = 17, Char = 18, Long = 20, UserDefinedType = 36, Array = 8192
- }
复制代码 这里的short对应vbsint interger对于vbslong |