Beispiel #1
0
        public static String GetCSharpTypeConstraintsClause(this MethodInfo mi)
        {
            Func <Type, IEnumerable <Type> > ogargs = t =>
                                                      t.Flatten(t1 => t1.XGetGenericArguments()).Where(garg => garg.IsGenericParameter);
            var m_ogargs = mi.Args().Concat(mi.ReturnType).SelectMany(ogargs).Distinct().ToArray();

            return(m_ogargs.Select(ogarg => ogarg.GetCSharpTypeConstraintsClause()).StringJoin(Environment.NewLine));
        }
Beispiel #2
0
        public static String GetCSharpTypeArgsClause(this MethodInfo mi)
        {
            Func <Type, IEnumerable <Type> > ogargs = t =>
                                                      t.Flatten(t1 => t1.XGetGenericArguments()).Where(garg => garg.IsGenericParameter);
            var involvedTypes = mi.Args().Concat(mi.ReturnType).Concat(mi.DeclaringType);
            var m_ogargs      = involvedTypes.SelectMany(ogargs).Distinct().ToArray();

            return(GetCSharpTypeArgsClause(m_ogargs));
        }
Beispiel #3
0
        public static MethodBuilder DefineOverride(this TypeBuilder t, MethodInfo @base)
        {
            MethodAttributes fixt = 0;
            var allAttrs          = Enum.GetValues(typeof(MethodAttributes)).Cast <MethodAttributes>();
            var validAttrs        = allAttrs
                                    .Where(a => a != MethodAttributes.Abstract)
                                    .Where(a => a != MethodAttributes.NewSlot);

            validAttrs.ForEach(a => fixt |= (a & @base.Attributes));

            var @override = t.DefineMethod(
                @base.Name,
                fixt,
                @base.ReturnType,
                @base.Args());

            return(@override);
        }
Beispiel #4
0
        public static MethodBuilder OverrideMethod(this TypeBuilder source, MethodInfo parentMethod, Func <ILGenerator, ILGenerator> body, IDictionary <MethodInfo, MethodBuilder> map)
        {
            var derived = source.DefineMethod(
                // that's an awesome idea but it hurts reflector and debuggability
                //                String.Format("{0}_{1}", parentMethod.Name, parentMethod.DeclaringType.ToShortString()),
                parentMethod.Name,
                MethodAttributes.Final | MethodAttributes.Public | MethodAttributes.Virtual,
                parentMethod.ReturnType,
                parentMethod.Args());

            if (body != null)
            {
                body(derived.il());
            }

            source.DefineMethodOverride(derived, parentMethod);
            if (map != null)
            {
                map[parentMethod] = derived;
            }
            return(derived);
        }
 public static int Argc(this MethodInfo mi)
 {
     return(mi.Args().Count());
 }
 public static bool IsOpenGeneric(this MethodInfo t)
 {
     return(t.ReturnType.IsOpenGeneric() ||
            t.Args().Any(pt => pt.IsOpenGeneric()) ||
            t.ContainsGenericParameters); // example: bool Meth<T>(int x);
 }