//TODO: Check type RepositoryId and TKey and TEntity public TRepository CreateRepositoryInstance <TRepository>(object repositorySource) { var template = new EntityFrameworkRepositoryTemplate(); var repositoryName = typeof(TRepository).Name.TrimStart('I', 'i'); var keyModel = repositorySource.GetType().GenericTypeArguments[0].GetProperties().FirstOrDefault(t => t.GetCustomAttribute <RepositoryKeyAttribute>() != null); if (keyModel == null) { throw new ArgumentException("Cant find attribute [RepositoryKey] in model"); } EntityFrameworkRepositoryTemplateParams entityFrameworkRepositoryTransferTemplateParams = new EntityFrameworkRepositoryTemplateParams { Name = repositoryName, Interface = typeof(TRepository).Name, NameSpace = typeof(TRepository).Namespace, FullNameModel = repositorySource.GetType().GenericTypeArguments[0].FullName, KeyName = keyModel.Name, KeyType = keyModel.PropertyType.FullName, Contract = ContractProvider.GenerateRepositoryContract(typeof(TRepository).GetInterfaces()) }; template.Session = new Dictionary <string, object> { { "Params", entityFrameworkRepositoryTransferTemplateParams } }; template.Initialize(); string code = template.TransformText(); using (CSharpCodeProvider compiler = new CSharpCodeProvider()) { var compileParams = new CompilerParameters { GenerateInMemory = true, }; var assemblyModule = repositorySource.GetType().GenericTypeArguments[0].Assembly.Modules.First(); compileParams.ReferencedAssemblies.AddRange(RequiredAssemblies.Select(t => Assembly.LoadWithPartialName(t).Modules.First().FullyQualifiedName).Concat(new[] { assemblyModule.FullyQualifiedName }).ToArray()); var result = compiler.CompileAssemblyFromSource(compileParams, code); var repositoryType = Array.Find(result.CompiledAssembly.GetTypes(), t => t.Name == repositoryName); return((TRepository)Activator.CreateInstance(repositoryType, new object[] { repositorySource })); } }
public override bool Execute() { try { using (var ps = System.Management.Automation.PowerShell.Create()) { ps.AddCommand("New-ModuleManifest"); if (AliasesToExport != null) { ps.AddParameter("AliasesToExport", AliasesToExport.Select(m => m.ItemSpec)); } ps.AddParameter("Author", Author.ItemSpec); if (ClrVersion != null) { ps.AddParameter("ClrVersion", ClrVersion.ItemSpec); } if (CmdletsToExport != null) { ps.AddParameter("CmdletsToExport", CmdletsToExport.Select(m => m.ItemSpec)); } ps.AddParameter("CompanyName", CompanyName.ItemSpec); ps.AddParameter("Copyright", Copyright.ItemSpec); ps.AddParameter("Description", Description.ItemSpec); if (DotNetFrameworkVersion != null) { ps.AddParameter("DotNetFrameworkVersion", DotNetFrameworkVersion.ItemSpec); } ps.AddParameter("FileList", FileList.Select(m => m.ItemSpec)); ps.AddParameter("FormatsToProcess", FormatsToProcess.Select(m => m.ItemSpec)); if (FunctionsToProcess != null) { ps.AddParameter("FunctionsToProcess", FunctionsToProcess.Select(m => m.ItemSpec)); } if (Guid != null) { ps.AddParameter("Guid", Guid.ItemSpec); } if (ModuleList != null) { ps.AddParameter("ModuleList", ModuleList.Select(m => m.ItemSpec)); } ps.AddParameter("ModuleToProcess", ModuleToProcess.ItemSpec); if (ModuleVersion != null) { ps.AddParameter("ModuleVersion", ModuleVersion.ItemSpec); } ps.AddParameter("NestedModules", NestedModules.Select(m => m.ItemSpec)); ps.AddParameter("Path", Path.ItemSpec); if (PowerShellHostName != null) { ps.AddParameter("PowerShellHostName", PowerShellHostName.ItemSpec); } if (PowerShellHostVersion != null) { ps.AddParameter("PowerShellHostVersion", PowerShellHostVersion.ItemSpec); } if (PowerShellVersion != null) { ps.AddParameter("PowerShellVersion", PowerShellVersion.ItemSpec); } if (ProcessorArchitecture != null) { ps.AddParameter("ProcessorArchitecture", ProcessorArchitecture.ItemSpec); } ps.AddParameter("RequiredAssemblies", RequiredAssemblies.Select(m => m.ItemSpec)); if (RequiredModules != null) { ps.AddParameter("RequiredModules", RequiredModules.Select(m => m.ItemSpec)); } if (ScriptsToProcess != null) { ps.AddParameter("ScriptsToProcess", ScriptsToProcess.Select(m => m.ItemSpec)); } ps.AddParameter("TypesToProcess", TypesToProcess.Select(m => m.ItemSpec)); if (VariablesToExport != null) { ps.AddParameter("VariablesToExport", VariablesToExport.Select(m => m.ItemSpec)); } ps.Invoke(); } } catch (Exception ex) { this.Log.LogErrorFromException(ex); return(false); } return(true); }