如果我有这样的:在.NET中获取泛型类型基名的正确方法是通过Substring?
Type t = typeof(Dictionary<String, String>);
如何获得"System.Collections.Generic.Dictionary"
作为一个字符串?是最好的/唯一的方法来做到这一点:
String n = t.FullName.Substring(0, t.FullName.IndexOf("`"));
虽然似乎有点hackish。
我想要这个的原因是我想要一个Type
对象,并生成类似于C#源代码文件中找到的代码。我产生了一些文字模板,我需要添加类型为字符串到源和FullName
财产产生这样的:
System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089]]
,而不是我想要的东西:
System.Collections.Generic.Dictionary<System.String, System.String>
编辑:好吧,这里是最终的代码,似乎仍然有点像一劈到我,但它的工作原理:
/// <summary>
/// This method takes a type and produces a proper full type name for it, expanding generics properly.
/// </summary>
/// <param name="type">
/// The type to produce the full type name for.
/// </param>
/// <returns>
/// The type name for <paramref name="type"/> as a string.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <para><paramref name="type"/> is <c>null</c>.</para>
/// </exception>
public static String TypeToString(Type type)
{
#region Parameter Validation
if (Object.ReferenceEquals(null, type))
throw new ArgumentNullException("type");
#endregion
if (type.IsGenericType)
{
if (type.GetGenericTypeDefinition() == typeof(Nullable<>))
{
Type underlyingType = type.GetGenericArguments()[0];
return String.Format("{0}?", TypeToString(underlyingType));
}
String baseName = type.FullName.Substring(0, type.FullName.IndexOf("`"));
return baseName + "<" + String.Join(", ", (from paramType in type.GetGenericArguments()
select TypeToString(paramType)).ToArray()) + ">";
}
else
{
return type.FullName;
}
}
正确的,但是是唯一的方法,通过使用子字符串中的通用部分之前得到名称的一部分,因为我发现,还是有一种整洁的反思方式,一种我不知道的财产或方法? – 2009-08-30 16:50:25
正如JohannesH在他的回答中指出的那样,你看到的是IL符号。如果你想要别的东西(比如C#),你需要自己映射它。我们有几个内部工具可以完成类似于您的建议。我不知道有任何其他方法来获得C#语法,但另一方面,我会假定格式被修复,因此您应该能够自己安全地转换它。 – 2009-08-30 17:04:16