public void WillNotDelegateANoScriptMethodMethod() { var delegater = new MethodDelegater(); var result = delegater.BuildDelegateMethod(typeof(TestDelegaterClass).GetMethod("NoScriptMethod"), "testClass"); result.ShouldBeNull(); }
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); }"); }
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); }"); }
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(); }"); }
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); }"); }
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); }