本文整理汇总了C#中Type.GetGenericArguments方法的典型用法代码示例。如果您正苦于以下问题:C# Type.GetGenericArguments方法的具体用法?C# Type.GetGenericArguments怎么用?C# Type.GetGenericArguments使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Type
的用法示例。
在下文中一共展示了Type.GetGenericArguments方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetGenericName
private static string GetGenericName(Type type) {
if (type.GetGenericArguments().Length == 0) {
return type.Name;
}
Type[] gArgs = type.GetGenericArguments();
string typeName = type.Name;
string pureTypeName = typeName.Substring(0, typeName.IndexOf('`'));
return pureTypeName + "<" + string.Join(",", GetGenericName(gArgs)) + ">";
}
示例2: CanEdit
public static bool CanEdit(Type type)
{
Debug.Assert(type != null);
if (type.IsGenericType &&
type.GetGenericTypeDefinition() == typeof(Nullable<>))
{
type = type.GetGenericArguments()[0];
}
return
type.IsEnum
|| type == typeof(System.String)
|| type == typeof(System.Char)
|| type == typeof(System.DateTime)
|| type == typeof(System.Boolean)
|| type == typeof(System.Byte)
|| type == typeof(System.Single)
|| type == typeof(System.Double)
|| type == typeof(System.Decimal)
|| type == typeof(System.Int16)
|| type == typeof(System.Int32)
|| type == typeof(System.Int64)
|| type == typeof(System.UInt16)
|| type == typeof(System.UInt32)
|| type == typeof(System.UInt64);
}
示例3: CodeTypeReference
public CodeTypeReference (Type baseType)
{
if (baseType == null) {
throw new ArgumentNullException ("baseType");
}
if (baseType.IsGenericParameter) {
this.baseType = baseType.Name;
this.referenceOptions = CodeTypeReferenceOptions.GenericTypeParameter;
}
else if (baseType.IsGenericTypeDefinition)
this.baseType = baseType.FullName;
else if (baseType.IsGenericType) {
this.baseType = baseType.GetGenericTypeDefinition ().FullName;
foreach (Type arg in baseType.GetGenericArguments ()) {
if (arg.IsGenericParameter)
TypeArguments.Add (new CodeTypeReference (new CodeTypeParameter (arg.Name)));
else
TypeArguments.Add (new CodeTypeReference (arg));
}
}
else if (baseType.IsArray) {
this.arrayRank = baseType.GetArrayRank ();
this.arrayElementType = new CodeTypeReference (baseType.GetElementType ());
this.baseType = arrayElementType.BaseType;
} else {
Parse (baseType.FullName);
}
this.isInterface = baseType.IsInterface;
}
示例4: GetTypeName
public static string GetTypeName(Type type) {
if(type == null) {
return "NULL";
}
if (type.IsGenericType) {
Type[] argTypes = type.GetGenericArguments();
StringBuilder builder = new StringBuilder(10);
string typeName = type.Name;
int tickIdx = typeName.IndexOf('`');
if (tickIdx != -1) {
typeName = typeName.Substring(0, tickIdx);
}
builder.Append(typeName);
builder.Append("<");
for (int i = 0; i < argTypes.Length; i++) {
builder.Append(GetTypeName(argTypes[i]));
if (i != argTypes.Length - 1) {
builder.Append(',');
}
}
builder.Append(">");
return builder.ToString();
}
return type.Name;
}
示例5: GetFriendlyName
public static string GetFriendlyName( Type type)
{
if (type == typeof(int))
return "int";
else if (type == typeof(short))
return "short";
else if (type == typeof(byte))
return "byte";
else if (type == typeof(bool))
return "bool";
else if (type == typeof(long))
return "long";
else if (type == typeof(float))
return "float";
else if (type == typeof(double))
return "double";
else if (type == typeof(decimal))
return "decimal";
else if (type == typeof(string))
return "string";
else if (type.IsGenericType)
return type.Name.Split('`')[0] + "<" + string.Join(", ", type.GetGenericArguments().Select(x => GetFriendlyName(x)).ToArray()) + ">";
else
return type.Name;
}
示例6: GeneratePageResult
private static object GeneratePageResult(HelpPageSampleGenerator sampleGenerator, Type type)
{
if (type.IsGenericType)
{
Type openGenericType = type.GetGenericTypeDefinition();
if (openGenericType == typeof(PageResult<>))
{
// Get the T in PageResult<T>
Type[] typeParameters = type.GetGenericArguments();
Debug.Assert(typeParameters.Length == 1);
// Create an enumeration to pass as the first parameter to the PageResult<T> constuctor
Type itemsType = typeof(List<>).MakeGenericType(typeParameters);
object items = sampleGenerator.GetSampleObject(itemsType);
// Fill in the other information needed to invoke the PageResult<T> constuctor
Type[] parameterTypes = new Type[] { itemsType, typeof(Uri), typeof(long?), };
object[] parameters = new object[] { items, null, (long)ObjectGenerator.DefaultCollectionSize, };
// Call PageResult(IEnumerable<T> items, Uri nextPageLink, long? count) constructor
ConstructorInfo constructor = type.GetConstructor(parameterTypes);
return constructor.Invoke(parameters);
}
}
return null;
}
示例7: GetNonNullableType
//CONFORMING
internal static Type GetNonNullableType(Type type)
{
if (IsNullableType(type))
{
return type.GetGenericArguments()[0];
}
return type;
}
示例8: GetTypeName
public static string GetTypeName(Type type)
{
Assert.IsTrue(type != null);
if (typeNames.ContainsKey(type))
{
return typeNames[type];
}
if (type.IsEnum)
{
string name = type.Name;
typeNames.Add(type, name);
return name;
}
string typeName = type.Name;
typeName = typeName.Substring(typeName.LastIndexOf('.') + 1);
typeName = typeName.Replace("`1", "");
typeName = typeName.Replace("`2", "");
typeName = typeName.Replace("`3", "");
if (type.IsGenericType)
{
if (typeName == "Nullable")
{
string ret = GetTypeName(type.GetGenericArguments()[0]) + "?";
typeNames.Add(type, ret);
return ret;
}
else
{
string ret = typeName + "<";
foreach (var entity in type.GetGenericArguments())
{
ret += GetTypeName(entity) + ", ";
}
ret = ret.Remove(ret.Length - 2) + ">";
typeNames.Add(type, ret);
return ret;
}
}
return type.Name;
}
示例9: From
internal override ProviderType From(Type type, int? size)
{
if(type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
type = type.GetGenericArguments()[0];
TypeCode tc = System.Type.GetTypeCode(type);
switch(tc)
{
case TypeCode.Boolean:
return SqlTypeSystem.Create(SqlDbType.Bit);
case TypeCode.Byte:
return SqlTypeSystem.Create(SqlDbType.TinyInt);
case TypeCode.SByte:
case TypeCode.Int16:
return SqlTypeSystem.Create(SqlDbType.SmallInt);
case TypeCode.Int32:
case TypeCode.UInt16:
return SqlTypeSystem.Create(SqlDbType.Int);
case TypeCode.Int64:
case TypeCode.UInt32:
return SqlTypeSystem.Create(SqlDbType.BigInt);
case TypeCode.UInt64:
return SqlTypeSystem.Create(SqlDbType.Decimal, 20, 0);
case TypeCode.Decimal:
return SqlTypeSystem.Create(SqlDbType.Decimal, 29, size ?? 4);
case TypeCode.Double:
return SqlTypeSystem.Create(SqlDbType.Float);
case TypeCode.Single:
return SqlTypeSystem.Create(SqlDbType.Real);
case TypeCode.Char:
return SqlTypeSystem.Create(SqlDbType.NChar, 1);
case TypeCode.String:
return GetBestType(SqlDbType.NVarChar, size);
case TypeCode.DateTime:
return SqlTypeSystem.Create(SqlDbType.DateTime);
case TypeCode.Object:
{
if(type == typeof(Guid))
return SqlTypeSystem.Create(SqlDbType.UniqueIdentifier);
if(type == typeof(byte[]) || type == typeof(Binary))
return GetBestType(SqlDbType.VarBinary, size);
if(type == typeof(char[]))
return GetBestType(SqlDbType.NVarChar, size);
if(type == typeof(TimeSpan))
return SqlTypeSystem.Create(SqlDbType.BigInt);
if(type == typeof(System.Xml.Linq.XDocument) ||
type == typeof(System.Xml.Linq.XElement))
return ProviderConstants.NTextType;
// else UDT?
return new SqlType(type);
}
default:
throw Error.UnexpectedTypeCode(tc);
}
}
示例10: NavigationPropertyConfiguration
public NavigationPropertyConfiguration(PropertyInfo property, EdmMultiplicity multiplicity)
: base(property)
{
if (property == null)
{
throw Error.ArgumentNull("property");
}
Multiplicity = multiplicity;
_relatedType = property.PropertyType;
if (multiplicity == EdmMultiplicity.Many)
{
//TODO: support use of a non-generic type i.e. public class OrdersCollection: List<Order>
if (!_relatedType.IsGenericType || _relatedType.GetGenericArguments().Length > 1)
{
throw Error.Argument("property", SRResources.ManyToManyNavigationPropertyMustReturnCollection);
}
_relatedType = _relatedType.GetGenericArguments()[0];
}
}
示例11: CreateModel
protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
{
// fallback to the type's default constructor
Type typeToCreate = modelType;
// we can understand some collection interfaces, e.g. IList<>, IDictionary<,>
if (modelType.IsGenericType)
{
Type genericTypeDefinition = modelType.GetGenericTypeDefinition();
if (genericTypeDefinition == typeof(IDictionary<,>))
{
typeToCreate = typeof(Dictionary<,>).MakeGenericType(modelType.GetGenericArguments());
}
else if (genericTypeDefinition == typeof(IEnumerable<>) || genericTypeDefinition == typeof(ICollection<>) || genericTypeDefinition == typeof(IList<>))
{
typeToCreate = typeof(List<>).MakeGenericType(modelType.GetGenericArguments());
}
}
try
{
if (ServiceLocator.Current.IsRegistered(typeToCreate))
return ServiceLocator.Current.GetInstance(typeToCreate);
return Activator.CreateInstance(typeToCreate);
}
catch (MissingMethodException exception)
{
// Ensure thrown exception contains the type name. Might be down a few levels.
//MissingMethodException replacementException =
// TypeHelpers.EnsureDebuggableException(exception, typeToCreate.FullName);
if (exception != null)
{
throw exception;
}
throw;
}
}
示例12: From
internal override ProviderType From(Type type, int? size)
{
if(type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
type = type.GetGenericArguments()[0];
// Retain mappings for DateTime and TimeSpan; add one for the new DateTimeOffset type.
//
if(System.Type.GetTypeCode(type) == TypeCode.Object &&
type == typeof(DateTimeOffset))
{
return SqlTypeSystem.Create(SqlDbType.DateTimeOffset);
}
return base.From(type, size);
}
示例13: AppendTypes
private static void AppendTypes(Type t, StringBuilder sb)
{
if (t.DeclaringType != null && !t.IsGenericParameter) {
AppendTypes(t.DeclaringType, sb);
sb.Append('+');
}
sb.Append(t.Name);
if (t.IsGenericType) {
sb.Append('[');
foreach (var gt in t.GetGenericArguments()) {
AppendTypes(gt, sb);
sb.Append(',');
}
--sb.Length;
sb.Append(']');
}
}
示例14: GetName
protected string GetName(Type type)
{
string name = type.Name;
if (type.IsGenericType || type.Name.IndexOf('_') > 0 && type.BaseType.IsGenericType)
{
if (!type.IsGenericType)
type = type.BaseType;
name = type.Name.Split('`')[0] + "<";
foreach (Type t in type.GetGenericArguments())
name += GetName(t) + ",";
name = name.TrimEnd(',') + ">";
}
return name;
}
示例15: AreAssignable
//CONFORMING
internal static bool AreAssignable(Type dest, Type src) {
if (dest == src) {
return true;
}
if (dest.IsAssignableFrom(src)) {
return true;
}
if (dest.IsArray && src.IsArray && dest.GetArrayRank() == src.GetArrayRank() && AreReferenceAssignable(dest.GetElementType(), src.GetElementType())) {
return true;
}
if (src.IsArray && dest.IsGenericType &&
(dest.GetGenericTypeDefinition() == typeof(System.Collections.Generic.IEnumerable<>)
|| dest.GetGenericTypeDefinition() == typeof(System.Collections.Generic.IList<>)
|| dest.GetGenericTypeDefinition() == typeof(System.Collections.Generic.ICollection<>))
&& dest.GetGenericArguments()[0] == src.GetElementType()) {
return true;
}
return false;
}