本文整理汇总了C#中IKVM.Reflection.Type.GetGenericArguments方法的典型用法代码示例。如果您正苦于以下问题:C# Type.GetGenericArguments方法的具体用法?C# Type.GetGenericArguments怎么用?C# Type.GetGenericArguments使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IKVM.Reflection.Type
的用法示例。
在下文中一共展示了Type.GetGenericArguments方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ContainsTypeBuilder
internal static bool ContainsTypeBuilder(Type type)
{
while (type.HasElementType)
{
type = type.GetElementType();
}
if (!type.IsGenericType || type.IsGenericTypeDefinition)
{
return type is TypeBuilder;
}
foreach (Type arg in type.GetGenericArguments())
{
if (ContainsTypeBuilder(arg))
{
return true;
}
}
return type.GetGenericTypeDefinition() is TypeBuilder;
}
示例2: GetWrapperFromAssemblyType
internal override TypeWrapper GetWrapperFromAssemblyType(Type type)
{
// we have to special case the fake types here
if (type.IsGenericType && !type.IsGenericTypeDefinition)
{
TypeWrapper outer = ClassLoaderWrapper.GetWrapperFromType(type.GetGenericArguments()[0]);
foreach (TypeWrapper inner in outer.InnerClasses)
{
if (inner.TypeAsTBD == type)
{
return inner;
}
foreach (TypeWrapper inner2 in inner.InnerClasses)
{
if (inner2.TypeAsTBD == type)
{
return inner2;
}
}
}
return null;
}
return base.GetWrapperFromAssemblyType(type);
}
示例3: 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;
//.........这里部分代码省略.........
示例4: CreateGenericParameters
TypeParameterSpec[] CreateGenericParameters (MetaType type, TypeSpec declaringType)
{
var tparams = type.GetGenericArguments ();
int parent_owned_count;
if (type.IsNested) {
parent_owned_count = type.DeclaringType.GetGenericArguments ().Length;
//
// System.Reflection duplicates parent type parameters for each
// nested type with slightly modified properties (eg. different owner)
// This just makes things more complicated (think of cloned constraints)
// therefore we remap any nested type owned by parent using `type_cache'
// to the single TypeParameterSpec
//
if (declaringType != null && parent_owned_count > 0) {
int read_count = 0;
while (read_count != parent_owned_count) {
var tparams_count = declaringType.Arity;
if (tparams_count != 0) {
var parent_tp = declaringType.MemberDefinition.TypeParameters;
read_count += tparams_count;
for (int i = 0; i < tparams_count; i++) {
import_cache.Add (tparams[parent_owned_count - read_count + i], parent_tp[i]);
}
}
declaringType = declaringType.DeclaringType;
}
}
} else {
parent_owned_count = 0;
}
if (tparams.Length - parent_owned_count == 0)
return null;
return CreateGenericParameters (parent_owned_count, tparams);
}
示例5: ResolveListTypes
internal void ResolveListTypes(Type type, ref Type itemType, ref Type defaultType)
{
if (type == null) return;
if(Helpers.GetTypeCode(type) != ProtoTypeCode.Unknown) return; // don't try this[type] for inbuilts
if(this[type].IgnoreListHandling) return;
// handle arrays
if (type.IsArray)
{
if (type.GetArrayRank() != 1)
{
throw new NotSupportedException("Multi-dimension arrays are supported");
}
itemType = type.GetElementType();
if (itemType == MapType(typeof(byte)))
{
defaultType = itemType = null;
}
else
{
defaultType = type;
}
}
// handle lists
if (itemType == null) { itemType = TypeModel.GetListItemType(this, type); }
// check for nested data (not allowed)
if (itemType != null)
{
Type nestedItemType = null, nestedDefaultType = null;
ResolveListTypes(itemType, ref nestedItemType, ref nestedDefaultType);
if (nestedItemType != null)
{
throw TypeModel.CreateNestedListsNotSupported();
}
}
if (itemType != null && defaultType == null)
{
#if WINRT
System.Reflection.TypeInfo typeInfo = System.Reflection.IntrospectionExtensions.GetTypeInfo(type);
if (typeInfo.IsClass && !typeInfo.IsAbstract && Helpers.GetConstructor(typeInfo, Helpers.EmptyTypes, true) != null)
#else
if (type.IsClass && !type.IsAbstract && Helpers.GetConstructor(type, Helpers.EmptyTypes, true) != null)
#endif
{
defaultType = type;
}
if (defaultType == null)
{
#if WINRT
if (typeInfo.IsInterface)
#else
if (type.IsInterface)
#endif
{
#if NO_GENERICS
defaultType = typeof(ArrayList);
#else
Type[] genArgs;
#if WINRT
if (typeInfo.IsGenericType && typeInfo.GetGenericTypeDefinition() == typeof(System.Collections.Generic.IDictionary<,>)
&& itemType == typeof(System.Collections.Generic.KeyValuePair<,>).MakeGenericType(genArgs = typeInfo.GenericTypeArguments))
#else
if (type.IsGenericType && type.GetGenericTypeDefinition() == MapType(typeof(System.Collections.Generic.IDictionary<,>))
&& itemType == MapType(typeof(System.Collections.Generic.KeyValuePair<,>)).MakeGenericType(genArgs = type.GetGenericArguments()))
#endif
{
defaultType = MapType(typeof(System.Collections.Generic.Dictionary<,>)).MakeGenericType(genArgs);
}
else
{
defaultType = MapType(typeof(System.Collections.Generic.List<>)).MakeGenericType(itemType);
}
#endif
}
}
// verify that the default type is appropriate
if (defaultType != null && !Helpers.IsAssignableFrom(type, defaultType)) { defaultType = null; }
}
}
示例6: IsReflectionOnly
internal static bool IsReflectionOnly(Type type)
{
while (type.HasElementType)
{
type = type.GetElementType();
}
Assembly asm = type.Assembly;
if (asm != null && asm.ReflectionOnly)
{
return true;
}
if (!type.IsGenericType || type.IsGenericTypeDefinition)
{
return false;
}
// we have a generic type instantiation, it might have ReflectionOnly type arguments
foreach (Type arg in type.GetGenericArguments())
{
if (IsReflectionOnly(arg))
{
return true;
}
}
return false;
}
示例7: GetName
// NOTE when this is called on a remapped type, the "warped" underlying type name is returned.
// E.g. GetName(typeof(object)) returns "cli.System.Object".
internal static string GetName(Type type)
{
Debug.Assert(!type.Name.EndsWith("[]") && !AttributeHelper.IsJavaModule(type.Module));
string name = type.FullName;
if (name == null)
{
// generic type parameters don't have a full name
return null;
}
if (type.IsGenericType && !type.ContainsGenericParameters)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append(MangleTypeName(type.GetGenericTypeDefinition().FullName));
sb.Append("_$$$_");
string sep = "";
foreach (Type t1 in type.GetGenericArguments())
{
Type t = t1;
sb.Append(sep);
// NOTE we can't use ClassLoaderWrapper.GetWrapperFromType() here to get t's name,
// because we might be resolving a generic type that refers to a type that is in
// the process of being constructed.
//
// For example:
// class Base<T> { }
// class Derived : Base<Derived> { }
//
while (ReflectUtil.IsVector(t))
{
t = t.GetElementType();
sb.Append('A');
}
if (PrimitiveTypeWrapper.IsPrimitiveType(t))
{
sb.Append(ClassLoaderWrapper.GetWrapperFromType(t).SigName);
}
else
{
string s;
if (ClassLoaderWrapper.IsRemappedType(t) || AttributeHelper.IsJavaModule(t.Module))
{
s = ClassLoaderWrapper.GetWrapperFromType(t).Name;
}
else
{
s = DotNetTypeWrapper.GetName(t);
}
// only do the mangling for non-generic types (because we don't want to convert
// the double underscores in two adjacent _$$$_ or _$$$$_ markers)
if (s.IndexOf("_$$$_") == -1)
{
s = s.Replace("__", "$$005F$$005F");
s = s.Replace(".", "__");
}
sb.Append('L').Append(s);
}
sep = "_$$_";
}
sb.Append("_$$$$_");
return sb.ToString();
}
if (AttributeHelper.IsNoPackagePrefix(type)
&& name.IndexOf('$') == -1)
{
return name.Replace('+', '$');
}
return MangleTypeName(name);
}
示例8: GetMissingType
internal static Type GetMissingType(Type type)
{
while (type.HasElementType)
{
type = type.GetElementType();
}
if (type.__IsMissing)
{
return type;
}
else if (type.__ContainsMissingType)
{
if (type.IsGenericType)
{
foreach (Type arg in type.GetGenericArguments())
{
Type t1 = GetMissingType(arg);
if (t1.__IsMissing)
{
return t1;
}
}
}
throw new NotImplementedException(type.FullName);
}
else
{
return type;
}
}
示例9: GetTypeName
void GetTypeName (StringBuilder sb, Type t)
{
sb.Append (RemoveGenericArity (t.Name));
sb.Append (FormatGenericParams (t.GetGenericArguments ()));
}
示例10: GenerateCode
void GenerateCode(Type type)
{
if (type.IsGenericType && !type.IsGenericTypeDefinition)
return; // generate nothing.
var implprops = new List<PropertyInfo> ();
var miscprops = new List<PropertyInfo> ();
foreach (var p in type.GetProperties (BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)) {
if (targets.Contains (p.PropertyType) && !p.PropertyType.IsEnum)
implprops.Add (p);
else
miscprops.Add (p);
}
output.WriteLine ("// generic ordinal type for " + type);
string template = @"
public {10}partial class {0} : {1} {2}
{{
{7} impl;
// constructor, if not abstract.
{9}
// impl-to-wrapper constructor
internal protected {8} ({7} impl) {6}
{{
this.impl = impl;
{11}
Initialize ();
}}
// initializer
void Initialize ()
{{
// repeat for all auto (new-Android-type) properties
{3}
}}
// explicit conversion operator
public static explicit operator {7} ({0} source)
{{
return source.impl;
}}
// For a property whose type is wrapper for Android type, just make it auto property (use private set for get-only ones)
{4}
// Non-Android properties follow.
{5}
}}
";
var actx = android_ass.GetType ("Android.Content.Context");
var aaset = android_ass.GetType ("Android.Util.IAttributeSet");
// somehow GetConstructor() returns weird results, so this workarounds that.
string args =
type.GetConstructors ().Any (c => c.GetParameters ().Length == 1 && c.GetParameters () [0].ParameterType.FullName == actx.FullName) ? "XamlView.CurrentContext" :
type.GetConstructors ().Any (c => c.GetParameters ().Length == 2 && c.GetParameters () [0].ParameterType.FullName == actx.FullName && c.GetParameters () [1].ParameterType.FullName == aaset.FullName) ? "XamlView.CurrentContext, null" :
"";
//if (type.Name == "SlidingDrawer") foreach (var ccc in type.GetConstructors ()) Console.WriteLine ("!!!! " + ccc + " / " + type.GetConstructor (new Type [] {actx}).GetParameters ().Length);
string publicConstructor = type.IsAbstract ? String.Empty : String.Format (@"
public {0} ()
: this (new {1} ({2}))
{{
}}", type.NonGenericName (), type.CSFullName (), args);
string templateInit = @"
if (impl.{0} != null)
{0} = Extensions.GetWrappedItem<{1}> (impl.{0});";
var isw = new StringWriter () { NewLine = "\n" };
foreach (var p in implprops)
if (!p.IsAbstract ())
isw.WriteLine (templateInit, p.Name, p.PropertyType.CSName ());
string templateImplProp = @"
public {3}{0} {1} {{ get; {2}set; }}";
var dpsw = new StringWriter () { NewLine = "\n" };
foreach (var p in implprops)
dpsw.WriteLine (templateImplProp, p.PropertyType.CSSwitchName (), p.Name, p.IsSetterPublic () ? null : "internal ", GetModifier (p));
string templateOrdProp1 = @"
public {3}{0} {1} {{
get {{ return {4}; }}
{2}
}}";
string templateOrdProp2 = @"
public {3}{0} {1} {{ get; {5} }}";
var nsw = new StringWriter () { NewLine = "\n" };
foreach (var p in miscprops) {
var setter = String.Format ("set {{ impl.{0} = value; }}", p.Name);
nsw.WriteLine (p.IsAbstract () ? templateOrdProp2 : templateOrdProp1, p.PropertyType.CSSwitchName (), p.Name, p.IsSetterPublic () ? setter : null, GetModifier (p), GetValueExpression (p), p.IsSetterPublic () ? "set;" : null);
}
string gconsts = null;
foreach (var arg in type.GetGenericArguments ()) {
var gca = String.Join (",", (from t in arg.GetGenericParameterConstraints () select t.CSSwitchName ()).ToArray ());
gconsts += String.IsNullOrEmpty (gca) ? null : "where " + arg.Name + " : " + gca;
}
//.........这里部分代码省略.........