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.
Related
I'm writing an external tool for AutoCAD (using the COM api) and I need to filter a SelectionSet, but I cant figure out the correct types for the "FilterType" and "FilterData" parameters.
I know it's outdated but I need to use the COM api and I can't for the life of me find any documentation. Can anyone with a bit more experience help me with this?
These are the problematic lines:
AcadApplication app = Marshal.GetActiveObject(AppID) as AcadApplication;
AcadSelectionSet SelectionSet = app.ActiveDocument.SelectionSets.Add(name);
int[] filterType = new int[1];
object[] filterData = new object[1];
filterType[0] = (int)DxfCode.BlockName;
filterData[0] = "T-siman";
SelectionSet.SelectOnScreen(filterType, filterData);
The last line returns the following exception:
System.ArgumentException: 'Invalid argument FilterType in SelectOnScreen'
I kept looking for a solution because I was stuck but then I found THIS QUESTION and noticed this person uses an array of type "short" instead of type "int" for the "FilterType" parameter. Doing this worked for me.
My working code:
AcadApplication app = Marshal.GetActiveObject(AppID) as AcadApplication;
AcadSelectionSet SelectionSet = app.ActiveDocument.SelectionSets.Add(name);
short[] filterType = new short[1];
object[] filterData = new object[1];
filterType[0] = (short)DxfCode.BlockName;
filterData[0] = "T-siman";
SelectionSet.SelectOnScreen(filterType, filterData);
I can compile a DLL at runtime from a simple code string without any issues. Opening it in IL Spy shows exactly what it should. But if I put any extension methods in the code it outputs the following error:
error CS1644: Feature `extension methods' cannot be used because it is not part of the C# 2.0 language specification
The only help I could find says I should be able to set the "CompilerVersion" to "v3.5" by providing a dictionary of options to the code provider, but it doesn't help at all.
Here's my code:
var options = new System.Collections.Generic.Dictionary<string, string> { { "CompilerVersion", "v3.5" } };
var codeProvider = new Microsoft.CSharp.CSharpCodeProvider(options);
var provider = System.CodeDom.Compiler.CodeDomProvider.CreateProvider("CSharp");
var parameters = new System.CodeDom.Compiler.CompilerParameters();
parameters.GenerateExecutable = false;
parameters.OutputAssembly = "AutoGen.dll";
var results = provider.CompileAssemblyFromSource(parameters, code);
Debug.Log(results.Errors.DeepToString());
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
I am trying to do highlighting on the search results. Here is the relevant part of my code.
QueryScorer scorer = new QueryScorer(q);
Lucene.Net.Search.Highlight.IFormatter formatter = new SimpleHTMLFormatter("<b>", "</b>");
Lucene.Net.Search.Highlight.Highlighter highlighter = new Highlighter(formatter, scorer);
highlighter.TextFragmenter = new SimpleFragmenter(800);
Lucene.Net.Util.Version vers = new Lucene.Net.Util.Version();
vers = Lucene.Net.Util.Version.LUCENE_30;
TokenStream stream = new StandardAnalyzer(vers).TokenStream(string.Empty, new StringReader(text));
string s = string.Empty;
try
{
s = highlighter.GetBestFragments(stream, text, 10, "...");
}
Here, GetBestFragments method throws a System.MissingMethodException.
I have tried to replace the original Lucene.net dll with Lucene.Net.Contrib but this time, I dont know what I should write instead of TokenStream. It doesnt exist in Lucene.Net.Contrib.* dlls.
I am working on existing code and I need to find out how I can rewrite TokenStream class and GetBestFragments method.
Thanx
The problem was something about deployment, that the new compatible Lucene.dll was replaced by the incompatible Sitecore7 dll.
So, if both lucene.net and lucene.net.contrib dll are referenced, it should work.
Not directly the solution to my question, but this source is worth mentioning again. (About lucene.dll versions) : http://laubplusco.net/sitecore-7-lucen-3-0-highlighted-results/
I am compiling a dynamic assembly at runtime. It needs to reference another dll. Everything works alright, as long as I set an OutputAssembly in my CompilerParameters. But as soon as I set GenerateInMemory = true; it fails:
var compilerParameters = new CompilerParameters();
if( compileInMemory )
compilerParameters.GenerateInMemory = true;
else
compilerParameters.OutputAssembly = "<my_dynamic_dll_path>";
compilerParameters.ReferencedAssemblies.Add( "<other_dll_path>" );
var compilerResults = new CSharpCodeProvider().CompileAssemblyFromDom( compilerParameters, codeCompileUnit );
// Here: compilerResults.Errors.HasErrors == false
foreach( var type in compilerResults.CompiledAssembly.GetTypes() )
{
// Exception:
// Unable to load one or more of the requested types.
// Retrieve the LoaderExceptions property for more information.
}
The LoaderExceptions are telling me that "other_dll" could not be found. Why is it working as long as I do not compile in-memory and what do I have to do to make it working in-memory?
There is no loading context when you use GenerateInMemory, the assembly gets loaded by the Assembly.Load(Byte[]) overload. One workaround is to temporarily hook the AppDomain.AssemblyResolve event so you can load "other_dll" yourself.