I have the following code:
return "[Inserted new " + typeof(T).ToString() + "]";
But
typeof(T).ToString()
returns the full name including namespace
Is there anyway to just get the class name (without any namespace qualifiers?)
typeof(T).Name // class name, no namespace
typeof(T).FullName // namespace and class name
typeof(T).Namespace // namespace, no class name
Try this to get type parameters for generic types:
public static string CSharpName(this Type type)
{
var sb = new StringBuilder();
var name = type.Name;
if (!type.IsGenericType) return name;
sb.Append(name.Substring(0, name.IndexOf('`')));
sb.Append("<");
sb.Append(string.Join(", ", type.GetGenericArguments()
.Select(t => t.CSharpName())));
sb.Append(">");
return sb.ToString();
}
Maybe not the best solution (due to the recursion), but it works. Outputs look like:
Dictionary<String, Object>
make use of (Type Properties)
Name Gets the name of the current member. (Inherited from MemberInfo.)
Example : typeof(T).Name;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With