本文整理汇总了C#中System.TypeSpec.MakeGenericType方法的典型用法代码示例。如果您正苦于以下问题:C# TypeSpec.MakeGenericType方法的具体用法?C# TypeSpec.MakeGenericType怎么用?C# TypeSpec.MakeGenericType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.TypeSpec
的用法示例。
在下文中一共展示了TypeSpec.MakeGenericType方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateType
TypeSpec CreateType (MetaType type, TypeSpec declaringType, DynamicTypeReader dtype, bool canImportBaseType)
{
TypeSpec spec;
if (import_cache.TryGetValue (type, out spec)) {
if (spec.BuiltinType == BuiltinTypeSpec.Type.Object) {
if (dtype.IsDynamicObject (this))
return module.Compiler.BuiltinTypes.Dynamic;
return spec;
}
if (!spec.IsGeneric || type.IsGenericTypeDefinition)
return spec;
if (!dtype.HasDynamicAttribute (this))
return spec;
// We've found same object in the cache but this one has a dynamic custom attribute
// and it's most likely dynamic version of same type IFoo<object> agains IFoo<dynamic>
// Do type resolve process again in that case
// TODO: Handle cases where they still unify
}
if (IsMissingType (type)) {
spec = new TypeSpec (MemberKind.MissingType, declaringType, new ImportedTypeDefinition (type, this), type, Modifiers.PUBLIC);
spec.MemberCache = MemberCache.Empty;
import_cache.Add (type, spec);
return spec;
}
if (type.IsGenericType && !type.IsGenericTypeDefinition) {
var type_def = type.GetGenericTypeDefinition ();
// Generic type definition can also be forwarded
if (compiled_types.TryGetValue (type_def, out spec))
return spec;
var targs = CreateGenericArguments (0, type.GetGenericArguments (), dtype);
if (declaringType == null) {
// Simple case, no nesting
spec = CreateType (type_def, null, new DynamicTypeReader (), canImportBaseType);
spec = spec.MakeGenericType (module, targs);
} else {
//
// Nested type case, converting .NET types like
// A`1.B`1.C`1<int, long, string> to typespec like
// A<int>.B<long>.C<string>
//
var nested_hierarchy = new List<TypeSpec> ();
while (declaringType.IsNested) {
nested_hierarchy.Add (declaringType);
declaringType = declaringType.DeclaringType;
}
int targs_pos = 0;
if (declaringType.Arity > 0) {
spec = declaringType.MakeGenericType (module, targs.Skip (targs_pos).Take (declaringType.Arity).ToArray ());
targs_pos = spec.Arity;
} else {
spec = declaringType;
}
for (int i = nested_hierarchy.Count; i != 0; --i) {
var t = nested_hierarchy [i - 1];
spec = MemberCache.FindNestedType (spec, t.Name, t.Arity);
if (t.Arity > 0) {
spec = spec.MakeGenericType (module, targs.Skip (targs_pos).Take (spec.Arity).ToArray ());
targs_pos += t.Arity;
}
}
string name = type.Name;
int index = name.IndexOf ('`');
if (index > 0)
name = name.Substring (0, index);
spec = MemberCache.FindNestedType (spec, name, targs.Length - targs_pos);
if (spec == null)
return null;
if (spec.Arity > 0) {
spec = spec.MakeGenericType (module, targs.Skip (targs_pos).ToArray ());
}
}
// Don't add generic type with dynamic arguments, they can interfere with same type
// using object type arguments
if (!spec.HasDynamicElement) {
// Add to reading cache to speed up reading
if (!import_cache.ContainsKey (type))
import_cache.Add (type, spec);
}
return spec;
}
Modifiers mod;
MemberKind kind;
//.........这里部分代码省略.........
示例2: DoResolveTypeParameters
protected override bool DoResolveTypeParameters()
{
instance_type = spec;
if (mutator != null)
instance_type = instance_type.MakeGenericType (this, mutator.MethodTypeParameters.Select (l => l.Type).ToArray ());
return true;
}
示例3: Inflate
public TypeSpec Inflate (TypeSpec ts)
{
var tp = ts as TypeParameterSpec;
if (tp != null)
return Inflate (tp);
var ac = ts as ArrayContainer;
if (ac != null) {
var et = Inflate (ac.Element);
if (et != ac.Element)
return ArrayContainer.MakeType (et, ac.Rank);
return ac;
}
//
// When inflating a nested type, inflate its parent first
// in case it's using same type parameters (was inflated within the type)
//
if (ts.IsNested) {
var parent = Inflate (ts.DeclaringType);
if (ts.DeclaringType != parent) {
//
// Keep the inflated type arguments
//
var targs = ts.TypeArguments;
//
// Parent was inflated, find the same type on inflated type
// to use same cache for nested types on same generic parent
//
// TODO: Should use BindingRestriction.DeclaredOnly or GetMember
ts = MemberCache.FindNestedType (parent, ts.Name, targs.Length);
//
// Handle the tricky case where parent shares local type arguments
// which means inflating inflated type
//
// class Test<T> {
// public static Nested<T> Foo () { return null; }
//
// public class Nested<U> {}
// }
//
// return type of Test<string>.Foo() has to be Test<string>.Nested<string>
//
if (targs.Length > 0) {
var inflated_targs = new TypeSpec [targs.Length];
for (var i = 0; i < targs.Length; ++i)
inflated_targs[i] = Inflate (targs[i]);
ts = ts.MakeGenericType (inflated_targs);
}
return ts;
}
}
// Inflate generic type
if (ts.Arity > 0)
return InflateTypeParameters (ts);
return ts;
}
示例4: InflateTypeParameters
//
// Inflates generic types
//
TypeSpec InflateTypeParameters (TypeSpec type)
{
var targs = new TypeSpec[type.Arity];
var i = 0;
var gti = type as InflatedTypeSpec;
//
// Inflating using outside type arguments, var v = new Foo<int> (), class Foo<T> {}
//
if (gti != null) {
for (; i < targs.Length; ++i)
targs[i] = Inflate (gti.TypeArguments[i]);
return gti.GetDefinition ().MakeGenericType (targs);
}
//
// Inflating parent using inside type arguments, class Foo<T> { ITest<T> foo; }
//
var args = type.MemberDefinition.TypeParameters;
foreach (var ds_tp in args)
targs[i++] = Inflate (ds_tp);
return type.MakeGenericType (targs);
}
示例5: CreateType
public TypeSpec CreateType (Type type, TypeSpec declaringType, ICustomAttributeProvider ca, int dynamicCursor, bool canImportBaseType)
{
TypeSpec spec;
if (import_cache.TryGetValue (type, out spec)) {
if (ca == null)
return spec;
if (type == typeof (object)) {
if (IsDynamicType (ca, dynamicCursor))
return InternalType.Dynamic;
return spec;
}
if (!spec.IsGeneric)
return spec;
#if NET_4_0
if (!ca.IsDefined (typeof (DynamicAttribute), false))
#endif
return spec;
// We've found same object in the cache but this one has a dynamic custom attribute
// and it's most likely dynamic version of same type IFoo<object> agains IFoo<dynamic>
// Do resolve the type process again in that case
}
if (type.IsGenericType && !type.IsGenericTypeDefinition) {
var type_def = type.GetGenericTypeDefinition ();
var targs = CreateGenericArguments (0, type.GetGenericArguments (), ca, dynamicCursor + 1);
if (declaringType == null) {
// Simple case, no nesting
spec = CreateType (type_def, null, null, 0, canImportBaseType);
spec = spec.MakeGenericType (targs);
} else {
//
// Nested type case, converting .NET types like
// A`1.B`1.C`1<int, long, string> to typespec like
// A<int>.B<long>.C<string>
//
var nested_hierarchy = new List<TypeSpec> ();
while (declaringType.IsNested) {
nested_hierarchy.Add (declaringType);
declaringType = declaringType.DeclaringType;
}
int targs_pos = 0;
if (declaringType.Arity > 0) {
spec = declaringType.MakeGenericType (targs.Skip (targs_pos).Take (declaringType.Arity).ToArray ());
targs_pos = spec.Arity;
} else {
spec = declaringType;
}
for (int i = nested_hierarchy.Count; i != 0; --i) {
var t = nested_hierarchy [i - 1];
spec = MemberCache.FindNestedType (spec, t.Name, t.Arity);
if (t.Arity > 0) {
spec = spec.MakeGenericType (targs.Skip (targs_pos).Take (spec.Arity).ToArray ());
targs_pos += t.Arity;
}
}
string name = type.Name;
int index = name.IndexOf ('`');
if (index > 0)
name = name.Substring (0, index);
spec = MemberCache.FindNestedType (spec, name, targs.Length - targs_pos);
if (spec.Arity > 0) {
spec = spec.MakeGenericType (targs.Skip (targs_pos).ToArray ());
}
}
// Don't add generic type with dynamic arguments, they can interfere with same type
// using object type arguments
if (!spec.HasDynamicElement) {
// Add to reading cache to speed up reading
if (!import_cache.ContainsKey (type))
import_cache.Add (type, spec);
}
return spec;
}
Modifiers mod;
MemberKind kind;
var ma = type.Attributes;
switch (ma & TypeAttributes.VisibilityMask) {
case TypeAttributes.Public:
case TypeAttributes.NestedPublic:
mod = Modifiers.PUBLIC;
break;
case TypeAttributes.NestedPrivate:
mod = Modifiers.PRIVATE;
break;
case TypeAttributes.NestedFamily:
mod = Modifiers.PROTECTED;
//.........这里部分代码省略.........