コード例 #1
0
        public void WillNotDelegateANoScriptMethodMethod()
        {
            var delegater = new MethodDelegater();

            var result = delegater.BuildDelegateMethod(typeof(TestDelegaterClass).GetMethod("NoScriptMethod"), "testClass");

            result.ShouldBeNull();
        }
コード例 #2
0
        public void CanDelegateAMethodWithParams()
        {
            var delegater = new MethodDelegater();

            var result = delegater.BuildDelegateMethod(typeof(TestDelegaterClass).GetMethod("MethodWithParams"), "testClass");

            CompileToString(result).ShouldEqual(@"public void MethodWithParams(params object[] objs)
{
    testClass.MethodWithParams(objs);
}");
        }
コード例 #3
0
        public void CanDelegateAMethodWithOptionalStringParameter()
        {
            var delegater = new MethodDelegater();

            var result = delegater.BuildDelegateMethod(typeof(TestDelegaterClass).GetMethod("MethodWithOptionalString"), "testClass");

            CompileToString(result).ShouldEqual(@"public void MethodWithOptionalString([System.Runtime.InteropServices.OptionalAttribute()] [System.Runtime.InteropServices.DefaultParameterValueAttribute(""Test"")] string str)
{
    testClass.MethodWithOptionalString(str);
}");
        }
コード例 #4
0
        public void CanDelegateAMethodWithReturnType()
        {
            var delegater = new MethodDelegater();

            var result = delegater.BuildDelegateMethod(typeof(TestDelegaterClass).GetMethod("MethodWithReturn"), "testClass");

            CompileToString(result).ShouldEqual(@"public string MethodWithReturn()
{
    return testClass.MethodWithReturn();
}");
        }
コード例 #5
0
        public void CanDelegateAMethodWithSimpleArgument()
        {
            var delegater = new MethodDelegater();

            var result = delegater.BuildDelegateMethod(typeof(TestDelegaterClass).GetMethod("MethodWithArgument"), "testClass");

            CompileToString(result).ShouldEqual(@"public void MethodWithArgument(string test)
{
    testClass.MethodWithArgument(test);
}");
        }
コード例 #6
0
        private void DelegateMethods(CodeTypeDeclaration typeDeclaration, Type[] delegatedTypes)
        {
            var initializeMethod = new CodeMemberMethod();

            initializeMethod.Name       = "Initialize";
            initializeMethod.Attributes = MemberAttributes.Public | MemberAttributes.Final;
            var codeParameter = new CodeParameterDeclarationExpression(typeof(object[]), "objs");

            initializeMethod.Parameters.Add(codeParameter);

            for (var i = 0; i < delegatedTypes.Length; i++)
            {
                var delegatedType = delegatedTypes[i];

                string instanceName = GenerateInstanceName(delegatedType);

                var fieldDeclaration = new CodeMemberField(delegatedType, instanceName);
                typeDeclaration.Members.Add(fieldDeclaration);

                foreach (var methodInfo in delegatedType.GetMethods(BindingFlags.Public | BindingFlags.Instance))
                {
                    if (methodInfo.DeclaringType == typeof(object) || methodInfo.Attributes.HasFlag(MethodAttributes.SpecialName))
                    {
                        continue;
                    }

                    var delegateMethod = _methodDelegater.BuildDelegateMethod(methodInfo, instanceName);

                    if (delegateMethod != null)
                    {
                        typeDeclaration.Members.Add(delegateMethod);
                    }
                }

                var argumentReference          = new CodeArgumentReferenceExpression("objs");
                var codeArrayIndexerExpression = new CodeArrayIndexerExpression(argumentReference, new CodeSnippetExpression(i.ToString()));

                var castExpression      = new CodeCastExpression(delegatedType, codeArrayIndexerExpression);
                var assignmentStatement = new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), instanceName), castExpression);

                initializeMethod.Statements.Add(assignmentStatement);
            }

            typeDeclaration.Members.Add(initializeMethod);
        }