public string GenerateModuleClassCode(IDynamicAssemblyBuilder dynamicAssemblyBuilder, IAssemblyLocator assemblyLocator, string moduleClassNamespace, string moduleClassName, IEnumerable <BindingConfigurationForFile> moduleServiceConfigurationElements) { // Lets add this assembly, since we are referencing DiHelper in auto-generated code. dynamicAssemblyBuilder.AddReferencedAssembly(typeof(NinjectDiManager)); dynamicAssemblyBuilder.AddReferencedAssembly(typeof(NinjectModule)); var moduleClassContents = new StringBuilder(); moduleClassContents.AppendLine("using System.Linq;"); moduleClassContents.AppendLine($"namespace {moduleClassNamespace}"); moduleClassContents.AppendLine("{"); moduleClassContents.AppendLine($"public class {moduleClassName} : {typeof(NinjectModule).FullName}"); moduleClassContents.AppendLine("{"); DiManagerImplementationHelper.AddCodeForOnDiContainerReadyMethod(moduleClassContents); // Add Load() method moduleClassContents.AppendLine("public override void Load()"); moduleClassContents.AppendLine("{"); foreach (var service in moduleServiceConfigurationElements) { AddServiceBindings(moduleClassContents, service, dynamicAssemblyBuilder); } moduleClassContents.AppendLine("}"); // End Load() method moduleClassContents.AppendLine("}"); moduleClassContents.AppendLine("}"); return(moduleClassContents.ToString()); }
/// <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) { 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);"); }