static void Demo5(CodeCompileUnit ccu)
        {
            // once again, we need one of these
            var res = new CodeDomResolver();

            res.CompileUnits.Add(ccu);
            res.Refresh();
            // we happen to know Program is the 1st type in the 2nd namespace*
            var prg = ccu.Namespaces[1].Types[0];
            // we need the scope where we're at
            var scope = res.GetScope(prg);
            // because our binder attaches to it
            var binder = new CodeDomBinder(scope);
            // now get all the methods with the specified name and flags
            var members = binder.GetMethodGroup(prg, "TestOverload", BindingFlags.Public | BindingFlags.Static);

            Console.WriteLine("There are {0} TestOverload method overloads.", members.Length);
            // try selecting one that takes a single string parameter
            var argTypes1 = new CodeTypeReference[] { new CodeTypeReference(typeof(string)) };
            var m         = binder.SelectMethod(BindingFlags.Public | BindingFlags.Static, members, argTypes1, null);

            if (null != m)
            {
                Console.WriteLine("Select TestOverload(string) returned:");
                _DumpMethod(m);
            }
            else
            {
                Console.WriteLine("Unable to bind to method");
            }
            // try selecting one that takes a single it parameter
            var argTypes2 = new CodeTypeReference[] { new CodeTypeReference(typeof(int)) };

            m = binder.SelectMethod(BindingFlags.Public | BindingFlags.Static, members, argTypes2, null);
            if (null != m)
            {
                Console.WriteLine("Select TestOverload(int) returned:");
                _DumpMethod(m);
            }
            else
            {
                Console.WriteLine("Unable to bind to method");
            }
            Console.WriteLine("Press any key...");
            Console.ReadKey();
            Console.Clear();
        }
        static void RunBinding()
        {
            // we'll need the resolver in a bit
            var res = new CodeDomResolver();

            // read the binding sample into the compile unit
            CodeCompileUnit ccu;

            using (var stm = File.OpenRead(@"..\..\Binding.cs"))
                ccu = SlangParser.ReadCompileUnitFrom(stm);

            // add the compile unit to the resolver
            res.CompileUnits.Add(ccu);

            // prepare the resolver
            res.Refresh();

            // get the first class available
            var tdecl = ccu.Namespaces[1].Types[0];
            // capture the scope at the typedecl level
            var scope = res.GetScope(tdecl);
            // create a new binder with that scope
            var binder = new CodeDomBinder(scope);
            // get the method group for Test(...)
            var methodGroup = binder.GetMethodGroup(tdecl, "Test", BindingFlags.Public | BindingFlags.Instance);

            // select the method that can take a string value
            var m = binder.SelectMethod(BindingFlags.Public, methodGroup, new CodeTypeReference[] { new CodeTypeReference(typeof(string)) }, null);

            Console.WriteLine(CU.ToString((CodeMemberMethod)m));

            // select the method that can take a short value
            // (closest match accepts int)
            m = binder.SelectMethod(BindingFlags.Public, methodGroup, new CodeTypeReference[] { new CodeTypeReference(typeof(short)) }, null);
            Console.WriteLine(CU.ToString((CodeMemberMethod)m));
        }
Beispiel #3
0
        static void _Patch(CodeMemberMethod meth, CodeDomVisitContext ctx, CodeDomResolver resolver)
        {
            if (null != meth)
            {
                // TODO: make sure the member is actually public
                if (null == meth.PrivateImplementationType)
                {
                    var scope  = resolver.GetScope(meth);
                    var td     = scope.DeclaringType;
                    var binder = new CodeDomBinder(scope);
                    for (int ic = td.BaseTypes.Count, i = 0; i < ic; ++i)
                    {
                        var ctr = td.BaseTypes[i];
                        var t   = resolver.TryResolveType(ctr, scope);
                        if (null != t)
                        {
                            var ma = binder.GetMethodGroup(t, meth.Name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
                            if (0 < ma.Length)
                            {
                                var isIface = false;
                                var ttd     = t as CodeTypeDeclaration;
                                if (null != ttd)
                                {
                                    isIface = ttd.IsInterface;
                                }
                                else
                                {
                                    var rrt = t as Type;
                                    isIface = rrt.IsInterface;
                                }
                                if (isIface)
                                {
                                    var m = binder.SelectMethod(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly, ma, _GetParameterTypes(meth.Parameters), null);
                                    if (null != m)
                                    {
                                        meth.ImplementationTypes.Add(ctr);
                                    }
                                }
                            }
                        }
                    }
                }

                meth.UserData.Remove("slang:unresolved");
                if ("Main" == meth.Name && (meth.Attributes & MemberAttributes.ScopeMask) == MemberAttributes.Static)
                {
                    if (0 == meth.Parameters.Count && null == meth.ReturnType || "System.Void" == meth.ReturnType.BaseType)
                    {
                        var epm = new CodeEntryPointMethod();
                        epm.Attributes = meth.Attributes;
                        epm.LinePragma = meth.LinePragma;
                        epm.StartDirectives.AddRange(meth.StartDirectives);
                        epm.EndDirectives.AddRange(meth.EndDirectives);
                        epm.Comments.AddRange(meth.Comments);
                        epm.CustomAttributes.AddRange(meth.CustomAttributes);
                        epm.ReturnTypeCustomAttributes.AddRange(meth.ReturnTypeCustomAttributes);
                        epm.TypeParameters.AddRange(meth.TypeParameters);
                        epm.PrivateImplementationType = meth.PrivateImplementationType;
                        epm.ImplementationTypes.AddRange(meth.ImplementationTypes);
                        epm.Name = meth.Name;
                        epm.Statements.AddRange(meth.Statements);
                        CodeDomVisitor.ReplaceTarget(ctx, epm);
                    }
                }
                //return;
            }
        }