I'm not sure what assembly I need to add, as it is showing SystemCore_EnumerableDebugView is missing but I have added the assembly System.Core dll. However, it is still showing the error that I need to add some reference.
List<clsDownloadAttachments> objlst = new List<clsDownloadAttachments>();
objlst = (List<clsDownloadAttachments>)Session["datsetRsult"];
var result = objlst.Where(p => p.mmid.ToString().Contains(mmidpk)).Select(p =>p.Filenames.ToString());
var rst = (new System.Linq.SystemCore_EnumerableDebugView<string>(result)).Items[0];
Please advise, thanks
Related
I have a use case in an application I'm writing where I have logic in an external DLL that is loaded dynamically. Now I need to add the ability to display shared views inside the ASP.NET MVC view that resides in the external DLL.
What I've done so far is to add the following in my ConfigureServices method:
UriBuilder uri = new UriBuilder(Assembly.GetEntryAssembly().CodeBase);
string fullPath = Uri.UnescapeDataString(uri.Path);
var mainDirectory = Path.GetDirectoryName(fullPath);
var assemblyFilePath = Path.Combine(mainDirectory, "MyLogic.dll");
var asmStream = File.OpenRead(assemblyFilePath);
var assembly = AssemblyLoadContext.Default.LoadFromStream(asmStream);
var part = new AssemblyPart(assembly);
services.AddControllersWithViews().ConfigureApplicationPartManager(apm => apm.ApplicationParts.Add(part));
This works fine as long as the DLL is added as a reference to the project. If I remove the reference then I'm getting an error in my application when I try to load the partial view:
InvalidOperationException: The partial view 'MyView' was not found. The following locations were searched: /Views/Consent/MyView.cshtml /Views/Shared/MyView.cshtml
What I tried doing is to list all known view of the application using the following code:
var feature = new ViewsFeature();
applicationPartManager.PopulateFeature(feature);
var views = feature.ViewDescriptors.Select(x => x.RelativePath).ToList();
What I see is that when I add the DLL as a reference in the project I see MyView.cshtml in the list, and if not then I don't see it - and the above error makes sense.
But my use case dictates that the loaded DLL is not referenced. Is there a way to add the views from it when it's not a reference?
Mystery solved... instead of ConfigureApplicationPartManager need to use AddApplicationPart like this:
UriBuilder uri = new UriBuilder(Assembly.GetEntryAssembly().CodeBase);
string fullPath = Uri.UnescapeDataString(uri.Path);
var mainDirectory = Path.GetDirectoryName(fullPath);
var assemblyFilePath = Path.Combine(mainDirectory, "Risco.Auth.Demo.Bridge.dll");
var asmStream = File.OpenRead(assemblyFilePath);
var assembly = AssemblyLoadContext.Default.LoadFromStream(asmStream);
services.AddControllersWithViews().AddApplicationPart(assembly);
i´m looking for an fixing of this error:
System.NotSupportedException: "Assembly.LoadFile is not supported in AppX."
I´ve got an Xamarin-Project and i ´m trying to create an instance of a type of mine. all in all it looks like this:
var assembly = Assembly.LoadFile("MicroChatsTest.dll");
var type = typeof(IUpdate);
var mytypes = assembly.GetTypes().Where(p => type.IsAssignableFrom(p)).FirstOrDefault();
if (mytypes == null) throw new Exception("E1234");
var whatever = Activator.CreateInstance(mytypes);
the error comes in the first line, because AppX/Xmarin doesn´t support the Assembly.LoadFile, is there any possibillity to fix this? thanks a lot.
Unfortunately, you cannot load non-AOT compiled assemblies in Xamarin:
https://forums.xamarin.com/discussion/2294/assembly-load-is-it-possible
I am trying to compile a string using CodeDom.
Dictionary<string, string> providerOptions = new Dictionary<string, string>
{
{"CompilerVersion", "v3.5"}
};
CSharpCodeProvider provider = new CSharpCodeProvider(providerOptions);
CompilerParameters compilerParams = new CompilerParameters
{
GenerateInMemory = true,
GenerateExecutable = false
};
compilerParams.ReferencedAssemblies.Add(Assembly.GetEntryAssembly().Location);
My problem is: In my compiled code I need to use classes from the project I am creating the code in. So I tried to add my current assembly as a reference, however it gives me the following error:
Metadata file 'Path\to\my\executable\MyProject.exe' could not be
opened -- 'An attempt was made to load a program with an incorrect
format.'
Anybody knows where my mistake is?
I had the same need and I solved this way:
compilerParams.ReferencedAssemblies.AddRange(Assembly.GetExecutingAssembly().GetReferencedAssemblies().Select(a => a.Name + ".dll").ToArray());
the +".dll" worked with me.
if you need to get the full path to the Assembly file you need to load them into a new AppDomain and then get the .Location of each one of them.
When I try to add a reference to an IronPython engine instance, the reference get's added to the references as expected. If I create another instance of the engine, the AddReference executes without an error, but the reference is not added to the references and import statements fail with "no module named ...".
var engine = Python.CreateEngine();
dynamic clr = engine.Runtime.GetClrModule();
clr.AddReference("IronPython.StdLib");
var references = (IEnumerable<Assembly>)clr.References;
Debug.Assert(references.Any(asm => asm.FullName.StartsWith("IronPython.StdLib"))); // ok
var source = "import pydoc\npydoc.plain(pydoc.render_doc(str))";
var result = engine.Execute<string>(source);
Debug.Assert(result.StartsWith("Python Library Documentation")); // ok
var engine2 = Python.CreateEngine();
dynamic clr2 = engine2.Runtime.GetClrModule();
clr2.AddReference("IronPython.StdLib");
var references2 = (IEnumerable<Assembly>)clr.References;
Debug.Assert(references2.Any(asm => asm.FullName.StartsWith("IronPython.StdLib"))); // fails
result = engine.Execute<string>(source); // throws ImportException "no module named pydoc"
Debug.Assert(result.StartsWith("Python Library Documentation"));
I tried with the binary release of IronPython 2.7.5 (installed to GAC) and C# 4.5, IronPython.StdLib is a precompiled DLL of the Python standard lib with pyc.
I also tried with self compiled IronPython 2.7.5 and 2.7.6 from github, but there the first engine.execute already fails with "no module named pydoc" although the reference gets added.
Am I doing something wrong or is it just a bug?
A colleague found out the reason for the failure. I post it here, in case someone else stumbles across this issue.
You need to load the assembly after adding the reference:
var engine = Python.CreateEngine();
dynamic clr = engine.Runtime.GetClrModule();
clr.AddReference("IronPython.StdLib");
// load assembly into engine
var assembly = Assembly.LoadFrom("IronPython.StdLib.dll");
engine.Runtime.LoadAssembly(assembly);
var references = (IEnumerable<Assembly>)clr.References;
Debug.Assert(references.Any(asm => asm.FullName.StartsWith("IronPython.StdLib"))); // ok
var source = "import pydoc\npydoc.plain(pydoc.render_doc(str))";
var result = engine.Execute<string>(source);
Debug.Assert(result.StartsWith("Python Library Documentation")); // ok
var engine2 = Python.CreateEngine();
dynamic clr2 = engine2.Runtime.GetClrModule();
clr2.AddReference("IronPython.StdLib");
// load assembly into engine2
engine2.Runtime.LoadAssembly(assembly);
var references2 = (IEnumerable<Assembly>)clr.References;
Debug.Assert(references2.Any(asm => asm.FullName.StartsWith("IronPython.StdLib"))); // does not fail
result = engine.Execute<string>(source); // does not throw any more
Debug.Assert(result.StartsWith("Python Library Documentation"));
I'm working on a code report project.
Currently, I'm able to compile the solution projects, get the diagnostics related to the compilation, etc..
The problem appears when I try to load my custom IDiagnosticAnalyzers, I've tried to use the AnalyzerFileReference and the AnalyzerImageReference without any result, Always I access the projects.Analizers are empty.
var inmutableArray = (new List<IDiagnosticAnalyzer>
{
new VariableEndedWithIdNamedCorrectlyDiagnosticAnalyzer()
}).ToImmutableArray();
var analyzerImageReference = new AnalyzerImageReference(inmutableArray);
foreach (Project project in solution.Projects)
{
project.AddAnalyzerReference(analyzerImageReference );
//No analizers loaded....
}
UPDATE (thanks for the feedback [Josh Varty])
I've tried this two ways:
var newProjects = new List<Project>();
foreach (Project project in solution.Projects)
{
var newSolutionn= solution.AddAnalyzerReference(project.Id, analyzerImageReference);
newProjects.Add(newSolutionn.Projects.FirstOrDefault(p=> p.Id == project.Id));
}
foreach (Project project in solution.Projects)
{
var newProject = project.AddAnalyzerReference( analyzerImageReference);
}
In both cases have the analyzers loaded but when I get the compilation and I get the diagnostics, I don't get the output related to this analyzers (I think they are not being called at the get compilation function).
var compilation = newProject.GetCompilationAsync().Result;
var diagnostics = compilation.GetDiagnostics();
Any suggestions?
As I commented, most Roslyn objects are immutable. This means methods like AddAnalyzerReference() don't mutate the project, but instead return a new one.
I don't have an analyzer to test this, but I believe you can use the following. Note that I'm using Solution.AddAnalyzerReference() instead of the one you were using.
var inmutableArray =(new List<IDiagnosticAnalyzer>
{
new VariableEndedWithIdNamedCorrectlyDiagnosticAnalyzer()
}).ToImmutableArray();
var analyzerImageReference = new AnalyzerImageReference(inmutableArray);
Solution newSolution = solution;
//We iterate over the original solution
foreach (Project project in solution.Projects)
{
//But we save our work in the newSolution
newSolution = newSolution.AddAnalyzerReference(project.Id, analyzerImageReference);
}
//Now newSolution should contain all your changes.
//Maybe you want to save this reference?
solution = newSolution;
I've found the way to do it:
public static Task<ImmutableArray<Diagnostic>> GetDiagnosticsAsync(this Compilation compilation, ImmutableArray<DiagnosticAnalyzer> analyzers, AnalyzerOptions options, CancellationToken cancellationToken = default(CancellationToken))
{
options = options ?? new AnalyzerOptions(ImmutableArray<AdditionalStream>.Empty, ImmutableDictionary<string, string>.Empty);
Compilation newCompilation = null;
var analyzerDriver = AnalyzerDriver.Create(compilation, analyzers, options, out newCompilation, cancellationToken);
newCompilation.GetDiagnostics(cancellationToken);
return analyzerDriver.GetDiagnosticsAsync();
}
I've published a version of the open source project that I've been working using Roslyn, you can see the code and other thing related to analyzers and codefix.
https://bitbucket.org/jrierapeiro/codeanalyzer
I had similar question which i answered over here.
You have to use compilation.WithAnalyzer(analyzer) and then getDiagnostics()