/// <inheritdoc /> public void GenerateCSharp(ICustomAutoGeneratedServiceInfo customAutoGeneratedServiceInfo, IDynamicAssemblyBuilder dynamicAssemblyBuilder, string generatedClassNamespace, string generatedClassName) { // Use IDynamicAssemblyBuilder.AddReferencedAssembly(string assemblyPath) or // IDynamicAssemblyBuilder.AddReferencedAssembly(Type typeInAssembly) to add assemblies that will be // referenced by auto-generated assembly if types in these assemblies are used in auto-generated code. dynamicAssemblyBuilder.AddReferencedAssembly(Path.Combine(Helpers.GetTestFilesFolderPath(), @"DynamicallyLoadedDlls\TestProjects.DynamicallyLoadedAssembly1.dll")); dynamicAssemblyBuilder.AddReferencedAssembly(typeof(IInterface1)); // By now Validate(ICustomAutoGeneratedServiceInfo customAutoGeneratedServiceInfo) already validated // that a single method with attribute is present in interface. var methodInfo = customAutoGeneratedServiceInfo.ImplementedInterface.GetMethods().First(); var attribute = (SimpleMethodMetadataAttribute)methodInfo.GetCustomAttributes().FirstOrDefault(x => x is SimpleMethodMetadataAttribute); var dynamicClass = dynamicAssemblyBuilder.StartDynamicallyGeneratedClass(generatedClassName, new[] { customAutoGeneratedServiceInfo.ImplementedInterface.GetTypeNameInCSharpClass() }, generatedClassNamespace); var methodData = dynamicClass.StartInterfaceImplementationMethod(methodInfo, false); methodData.AddCodeLine("{"); methodData.AddCodeLine("var testReferencedAssembly = new DynamicallyLoadedAssembly1.Dog(40);"); methodData.AddCodeLine($"return {attribute.ReturnedValue};"); methodData.AddCodeLine("}"); }
public void GenerateCSharp(ICustomAutoGeneratedServiceInfo customAutoGeneratedServiceInfo, IDynamicAssemblyBuilder dynamicAssemblyBuilder, string generatedClassNamespace, string generatedClassName) { GenerateCSharpCode(dynamicAssemblyBuilder.StartDynamicallyGeneratedClass(generatedClassName, new[] { customAutoGeneratedServiceInfo.ImplementedInterface.GetTypeNameInCSharpClass() }, generatedClassNamespace)); }
public void GenerateCSharp(ICustomAutoGeneratedServiceInfo customAutoGeneratedServiceInfo, IDynamicAssemblyBuilder dynamicAssemblyBuilder, string generatedClassNamespace, string generatedClassName) { if (_simulateGenerateCSharpFailure) { throw new Exception($"{nameof(GenerateCSharp)} failed."); } var dynamicallyGeneratedClass = dynamicAssemblyBuilder.StartDynamicallyGeneratedClass(generatedClassName, new [] { customAutoGeneratedServiceInfo.ImplementedInterface.FullName }, generatedClassNamespace); dynamicallyGeneratedClass.AddCode("public override int GetColor() => 10"); if (!_simulateAssemblyBuildFailure) { dynamicallyGeneratedClass.AddCode(";"); } }
public void GenerateCSharp(ICustomAutoGeneratedServiceInfo customAutoGeneratedServiceInfo, IDynamicAssemblyBuilder dynamicAssemblyBuilder, string generatedClassNamespace, string generatedClassName) { dynamicAssemblyBuilder.AddReferencedAssembly( Path.Combine(Helpers.GetTestFilesFolderPath(), @"DynamicallyLoadedDlls\TestProjects.DynamicallyLoadedAssembly1.dll")); dynamicAssemblyBuilder.AddReferencedAssembly( typeof(SharedServices.Interfaces.IInterface1)); var classInfo = dynamicAssemblyBuilder.StartDynamicallyGeneratedClass( generatedClassName, generatedClassNamespace); var methodInfo = classInfo.StartMethod("GetValue", typeof(int), Array.Empty <IMethodParameterInfo>(), AccessLevel.Public, false, false); methodInfo.AddCodeLine("=> 10;"); }
public void GenerateCSharp(ICustomAutoGeneratedServiceInfo customAutoGeneratedServiceInfo, IDynamicAssemblyBuilder dynamicAssemblyBuilder, string generatedClassNamespace, string generatedClassName) { var dynamicClass = dynamicAssemblyBuilder.StartDynamicallyGeneratedClass(generatedClassName, new[] { customAutoGeneratedServiceInfo.ImplementedInterface.GetTypeNameInCSharpClass() }, generatedClassNamespace); foreach (var referencedAssemblyPath in ReferencedAssemblyPathsToAddOnCodeGeneration) { dynamicAssemblyBuilder.AddReferencedAssembly(referencedAssemblyPath); } var methodInfo = customAutoGeneratedServiceInfo.ImplementedInterface.GetMethods() .First(x => x.Name == nameof(IDogWeightsCalculator.GetDogWeightInKilograms)); var methodData = dynamicClass.StartInterfaceImplementationMethod(methodInfo, false); methodData.AddCodeLine("=> (int)(0.453592 * (new DynamicallyLoadedAssembly1.Dog(40)).Weight);"); }