private CilType ProcessType(IKVM.Reflection.Type type) { var result = new CilType { Name = type.Name, Namespace = type.Namespace, BaseType = type.BaseType != null ? type.BaseType.FullName : null, ReflectionType = type }; var methods = new List <CilMethod>(); result.Methods = methods; var flags = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static; foreach (var method in type.GetMethods(flags)) { if (method.DeclaringType == type) { methods.Add(ProcessMethod(result, method)); } } foreach (var ctor in type.GetConstructors(flags)) { if (ctor.DeclaringType == type) { methods.Add(ProcessMethod(result, ctor)); } } return(result); }
protected IEnumerable <ProcessedConstructor> GetConstructors(ProcessedType processedType) { Type t = processedType.Type; foreach (var ctor in t.GetConstructors()) { // .cctor not to be called directly by native code if (ctor.IsStatic) { continue; } if (!ctor.IsPublic) { continue; } bool pcheck = true; foreach (var p in ctor.GetParameters()) { var pt = p.ParameterType; if (!IsSupported(pt)) { Delayed.Add(ErrorHelper.CreateWarning(1020, $"Constructor `{ctor}` is not generated because of parameter type `{pt}` is not supported.")); pcheck = false; } else if (p.HasDefaultValue) { members_with_default_values.Add(ctor); } } if (!pcheck) { continue; } yield return(new ProcessedConstructor(ctor, this, processedType)); } }
protected IEnumerable <ConstructorInfo> GetConstructors(Type t) { foreach (var ctor in t.GetConstructors()) { // .cctor not to be called directly by native code if (ctor.IsStatic) { continue; } if (!ctor.IsPublic) { continue; } bool pcheck = true; foreach (var p in ctor.GetParameters()) { var pt = p.ParameterType; if (!IsSupported(pt)) { delayed.Add(ErrorHelper.CreateWarning(1020, $"Constructor `{ctor}` is not generated because of parameter type `{pt}` is not supported.")); pcheck = false; } else if (p.HasDefaultValue) { delayed.Add(ErrorHelper.CreateWarning(1021, $"Constructor `{ctor}` parameter `{p.Name}` has a default value that is not supported.")); } } if (!pcheck) { continue; } yield return(ctor); } }
public void OutlineType() { bool first; OutlineAttributes(); o.Write(GetTypeVisibility(t)); if (t.IsClass && !t.IsSubclassOf(type_multicast_delegate)) { if (t.IsSealed) { o.Write(t.IsAbstract ? " static" : " sealed"); } else if (t.IsAbstract) { o.Write(" abstract"); } } o.Write(" "); o.Write(GetTypeKind(t)); o.Write(" "); Type [] interfaces = (Type [])Comparer.Sort(TypeGetInterfaces(t, declared_only)); Type parent = t.BaseType; if (t.IsSubclassOf(type_multicast_delegate)) { MethodInfo method; method = t.GetMethod("Invoke"); o.Write(FormatType(method.ReturnType)); o.Write(" "); o.Write(GetTypeName(t)); o.Write(" ("); OutlineParams(method.GetParameters()); o.Write(")"); WriteGenericConstraints(t.GetGenericArguments()); o.WriteLine(";"); return; } o.Write(GetTypeName(t)); if (((parent != null && parent != type_object && parent != type_value_type) || interfaces.Length != 0) && !t.IsEnum) { first = true; o.Write(" : "); if (parent != null && parent != type_object && parent != type_value_type) { o.Write(FormatType(parent)); first = false; } foreach (Type intf in interfaces) { if (!first) { o.Write(", "); } first = false; o.Write(FormatType(intf)); } } if (t.IsEnum) { Type underlyingType = t.GetEnumUnderlyingType(); if (underlyingType != type_int) { o.Write(" : {0}", FormatType(underlyingType)); } } WriteGenericConstraints(t.GetGenericArguments()); o.WriteLine(" {"); o.Indent++; if (t.IsEnum) { bool is_first = true; foreach (FieldInfo fi in t.GetFields(BindingFlags.Public | BindingFlags.Static)) { if (!is_first) { o.WriteLine(","); } is_first = false; o.Write(fi.Name); } o.WriteLine(); o.Indent--; o.WriteLine("}"); return; } first = true; foreach (ConstructorInfo ci in t.GetConstructors(DefaultFlags)) { if (!ShowMember(ci)) { continue; } if (first) { o.WriteLine(); } first = false; OutlineMemberAttribute(ci); OutlineConstructor(ci); o.WriteLine(); } first = true; foreach (MethodInfo m in Comparer.Sort(t.GetMethods(DefaultFlags))) { if (!ShowMember(m)) { continue; } if ((m.Attributes & MethodAttributes.SpecialName) != 0) { continue; } if (first) { o.WriteLine(); } first = false; OutlineMemberAttribute(m); OutlineMethod(m); o.WriteLine(); } first = true; foreach (MethodInfo m in t.GetMethods(DefaultFlags)) { if (!ShowMember(m)) { continue; } if ((m.Attributes & MethodAttributes.SpecialName) == 0) { continue; } if (!(m.Name.StartsWith("op_"))) { continue; } if (first) { o.WriteLine(); } first = false; OutlineMemberAttribute(m); OutlineOperator(m); o.WriteLine(); } first = true; foreach (PropertyInfo pi in Comparer.Sort(t.GetProperties(DefaultFlags))) { if (!((pi.CanRead && ShowMember(pi.GetGetMethod(true))) || (pi.CanWrite && ShowMember(pi.GetSetMethod(true))))) { continue; } if (first) { o.WriteLine(); } first = false; OutlineMemberAttribute(pi); OutlineProperty(pi); o.WriteLine(); } first = true; foreach (FieldInfo fi in t.GetFields(DefaultFlags)) { if (!ShowMember(fi)) { continue; } if (first) { o.WriteLine(); } first = false; OutlineMemberAttribute(fi); OutlineField(fi); o.WriteLine(); } first = true; foreach (EventInfo ei in Comparer.Sort(t.GetEvents(DefaultFlags))) { if (!ShowMember(ei.GetAddMethod(true))) { continue; } if (first) { o.WriteLine(); } first = false; OutlineMemberAttribute(ei); OutlineEvent(ei); o.WriteLine(); } first = true; foreach (Type ntype in Comparer.Sort(t.GetNestedTypes(DefaultFlags))) { if (!ShowMember(ntype)) { continue; } if (first) { o.WriteLine(); } first = false; #if STATIC new Outline(universe, mscorlib, ntype, o, declared_only, show_private, filter_obsolete).OutlineType(); #else new Outline(ntype, o, declared_only, show_private, filter_obsolete).OutlineType(); #endif } o.Indent--; o.WriteLine("}"); }
internal static ConstructorInfo[] GetConstructors(Type type, bool nonPublic) { return type.GetConstructors( nonPublic ? BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic : BindingFlags.Instance | BindingFlags.Public); }
internal static void GetConstructors(Type type, out ConstructorInfo defCtor, out ConstructorInfo singleOneArgCtor) { defCtor = null; int oneArgCtorCount = 0; ConstructorInfo oneArgCtor = null; ConstructorInfo[] constructors = type.GetConstructors(BindingFlags.Public | BindingFlags.Instance); // HACK we have a special rule to make some additional custom attributes from mscorlib usable: // Attributes that have two constructors, one an enum and another one taking a byte, short or int, // we only expose the enum constructor. if (constructors.Length == 2 && type.Assembly == Types.Object.Assembly) { ParameterInfo[] p0 = constructors[0].GetParameters(); ParameterInfo[] p1 = constructors[1].GetParameters(); if (p0.Length == 1 && p1.Length == 1) { Type t0 = p0[0].ParameterType; Type t1 = p1[0].ParameterType; bool swapped = false; if (t1.IsEnum) { Type tmp = t0; t0 = t1; t1 = tmp; swapped = true; } if (t0.IsEnum && (t1 == Types.Byte || t1 == Types.Int16 || t1 == Types.Int32)) { if (swapped) { singleOneArgCtor = constructors[1]; } else { singleOneArgCtor = constructors[0]; } return; } } } if (type.Assembly == Types.Object.Assembly) { if (type.FullName == "System.Runtime.CompilerServices.MethodImplAttribute") { foreach (ConstructorInfo ci in constructors) { ParameterInfo[] p = ci.GetParameters(); if (p.Length == 1 && p[0].ParameterType.IsEnum) { singleOneArgCtor = ci; return; } } } } foreach (ConstructorInfo ci in constructors) { ParameterInfo[] args = ci.GetParameters(); if (args.Length == 0) { defCtor = ci; } else if (args.Length == 1) { if (IsSupportedType(args[0].ParameterType)) { oneArgCtor = ci; oneArgCtorCount++; } else { // set to two to make sure we don't see the oneArgCtor as viable oneArgCtorCount = 2; } } } singleOneArgCtor = oneArgCtorCount == 1 ? oneArgCtor : null; }
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; } // FIXME: write custom attributes bool callBase = targets.Contains (type.BaseType); output.WriteLine (template, type.CSName (), type.BaseType.CSSwitchName (), gconsts, isw, dpsw, nsw, callBase ? " : base (impl)" : null, type.CSFullName (), type.NonGenericName (), publicConstructor, type.IsAbstract ? "abstract " : null, callBase ? null : "XamlView.Register (impl, this);"); }