简体   繁体   English

从类型名称获取可为空的类型

[英]Get a nullable type from a type name

I have the following scenario where I get a string that looks as follows 我有以下情况,其中我得到一个字符串,如下所示

"System.DateTime?"

or 要么

"int?"

What I would like to be able to do, is retrieve the System.Type for that string which would look like: 我想做的是,检索该字符串的System.Type,如下所示:

{Name = "Nullable 1" FullName = "System.Nullable 1[[System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]"} {Name =“ Nullable 1" FullName = "System.Nullable 1 [[System.DateTime,mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089]]]}}

Now I know how to retrieve the type from a string I can just say Type.GetType("System.DateTime") when dealing with non nullable type or typeof(DateTime?) when I know it's going to be DateTime nullable, but in this instance I'm unaware of the what nullable type might come through and will only receive it as string. 现在,我知道了如何从字符串中检索类型,当我处理不可为null的类型时,我只能说Type.GetType("System.DateTime") ,当我知道它将为DateTime可为null时,可以说是typeof(DateTime?) ,但是在此实例我不知道会出现什么可为null的类型,只能将其作为字符串接收。

I'm missing part of the context - for example, does the string always represent a nullable value type? 我缺少上下文的一部分-例如,字符串是否始终表示可为空的值类型?

Given that you can extract the value type name from the string and create a reference to that type: 假设您可以从字符串中提取值类型名称并创建对该类型的引用:

var valueType = Type.GetType("System.DateTime");
var nullableType = typeof(Nullable<>).MakeGenericType(valueType);

That should solve part of the problem. 那应该解决部分问题。 The harder part will be determining the underlying type from the string, which depends on what sort of inputs you're receiving. 最难的部分是从字符串中确定基础类型,这取决于您要接收的输入类型。 It's easy to get a type from System.DateTime , but harder from int . System.DateTime获取类型很容易,但是从int获取类型则很困难。 This answer may help with that part. 这个答案可能对那部分有帮助。


As far as the input - is there any option that you could provide a list of available choices to the user so that input you receive would always be a valid type name? 至于输入-是否有任何选项可以向用户提供可用选项列表,以便您收到的输入始终是有效的类型名称? That would negate the need for any of this. 那将不需要任何这些。 Then you could just do Type.GetType(stringWithTypeName) . 然后,您可以执行Type.GetType(stringWithTypeName)

In addition to Scott's answer, you could also use the Type.GetType with the correct name: 除了Scott的答案外,您还可以使用Type.GetType和正确的名称:

System.Nullable`1[System.DateTime]

So this function will get back the correct type for you. 因此,此函数将为您找回正确的类型。

public Type GetType(string typeString)
{
    if (typeString.EndsWith("?"))
    {
        typeString = $"System.Nullable`1[{typeString.TrimEnd('?')}]";
    }
    return Type.GetType(typeString);
}

One way to solve this is by creating an extension method for type "Type" and make the implementation of that method to check if it is a nullable type and return the right type. 解决此问题的一种方法是为类型“ Type”创建扩展方法,并使该方法的实现检查其是否为可空类型并返回正确的类型。 follows example: 如下示例:

namespace ExtensionMethod  
    {
        public static class MyExtensions
        {
            public static Type GetNullabeType(this Type t, string typeName)
            {
                string typeString = typeName;
                var typeNonNullabe = Type.GetType(typeString.Replace("?", ""));
                if (typeString.Contains("?") && typeNonNullabe != null)
                {
                    return typeof(Nullable<>).MakeGenericType(typeNonNullabe);
                }
                return t;
            }
        }
    }

after that, just use this extension method as follows: 之后,只需使用如下扩展方法:

Type t = null;
var type = t.GetNullabeType("System.DateTime?");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM