Compiling a standalone C# file at runtime which references another file - c#

Using the code in this link I am attempting to compile a generated .cs file at runtime. The file (call it Gen.cs) has been generated by CodeDOM and subclasses another file (call it Base.cs). I get the following error message from the runtime compilation:
The type or namespace name `Base' could not be found. Are you missing a using directive or an assembly reference?
I know that this error means it can't find Base.cs when it's compiling. I'm new to both C# and compiling C# files dynamically, so this is a bit hard to pick apart. Neither Base nor Gen use namespaces, and both .cs files are in the same directory. Is there anything I'm missing? Why can't Gen.cs see Base.cs?
EDIT - This may or may not be relevant, but here's the result of a manual compilation via gmcs (I'm on MacOS)
mtrc$ gmcs -lib:Base.cs Gen.cs
Gen.cs(13,27): error CS0246: The type or namespace name
`Base' could not be found. Are you missing a using
directive or an assembly reference?

Typically a compiler will only compile source files that it's been told to compile in a single pass. ie: specify all .cs names on the command line, or specify to compile all .cs files in a given folder.
In this case:
string []arrCode = { code, "base.cs" };
CompilerResults results =
compiler.CompileAssemblyFromSource(compilerparams, arrCode);
Alternative syntax:
CompilerResults results =
compiler.CompileAssemblyFromSource(compilerparams, code, "base.cs");

Related

Finding where I need to update the naming Blazor WebAssembly

I renamed my project, and updated it to .Net 5. Now, I obviously havent catched all the occurances since I get this error:
\source\repos\BlazorBattles\BlazorBattles\Server\obj\Debug\net5.0\Razor\Pages\Error.cshtml.g.cs(78,71): error CS0246: The type or namespace name 'blazor_battles' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\hagenek\source\repos\BlazorBattles\BlazorBattles\Server\BlazorBattles.Server.csproj]
However, it is hard to locate it, because the error shows a file that is being generated by the project ,not a file that is in the core project.
Any tips?
For some reason visual studio could not locate the string "blazor_battles" in the Error.cshtml file inside of Server/Pages directory.
Using hyphens in your project names creates problems. Don't do it.

Using C# DLL in managed C++ (command line)

I've looked over a few questions with a title just like this one, but they either do not talk about command line, or don't seem to work for me for some reason. From what I have read, it seemed as if I could "simply" do the following:
The dummy code (C#):
using System;
public static class Foo {
public static void Bar() {
Console.WriteLine("o3o");
}
}
More dummy code (Visual C++):
#using <test.dll>
int main() {
Foo::Bar();
return 0;
}
C# DLL compiled using:
csc /out:test.dll /t:library src\cs\Foo.cs
Visual C++ object file compiled using:
cl /Ox /clr /AI. /c src\vc\test.cpp
Executable compiled using:
link /out:test.exe test.obj
The following exception is thrown upon running the executable:
Unhandled Exception: System.TypeLoadException: Could not load type 'Foo' from assembly 'test, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
at main()
at mainCRTStartup()
I have a strong feeling that I was supposed to also reference the DLL in the link phase, but I couldn't find any option for linking a DLL similar to GCC's -l option. Attempting to pass the DLL along with the .obj to link causes it to tell me that linking assemblies is not supported. Interestingly, though, if I build a .netmodule instead of a DLL, i.e. by:
csc /out:test.dll /t:module src\cs\Foo.cs
and changing the #using directive to
#using <test.netmodule>
the executable runs without error. This feels a bit weird, for I don't think anybody packages code in .netmodules (what are .netmodules, anyway?).
All of your steps should have worked, but one very simple issue is preventing your program from running.
Namely: Your C# DLL assembly name is test, and your C++/CLI exe assembly has the same name. They both have the same identity.
So when looking for Foo::Bar in the test assembly, the loader first checks if the assembly is loaded in the AppDomain. It is - it's your C++/CLI exe, and you can't have several assemblies with the same identity loaded simultaneously within the same AppDomain. Your C# dll wasn't even given a try.
Just change either one of them and everything will work fine:
link /out:test2.exe test.obj
As for what's a .netmodule, it's the format used for linking managed code statically, that's why you managed to link your C# code with your C++/CLI code without issues. It's roughly the equivalent of a .lib file for managed code.
And you're right, it's not used very often.

Linking a resource file to an existing .NET assembly using Assembly Linker

I am trying to link the file name.ext to assembly.dll using the AL (Assembly Linker) to mimic what can be done in C++ using the /ASSEMBLYLINKRESOURCE option.
The syntax I am using is the following:
al.exe assembly.dll /link:name.ext
and I get the following error:
ALINK: error AL1017: No target filename was specified
I also tried:
al.exe assembly.dll /out:outAssembly.dll /link:name.ext
getting the following error:
ALINK: warning AL1020: Ignoring included assembly 'assembly.dll'
ALINK: error AL1019: Metadata failure while creating assembly -- The system cannot find the file specified.
What I am doing wrong? What is the correct syntax to obtain the resource link?
Thanks.
The Assembly Linker generates a file that has an assembly manifest from one or more files that are either modules or resource files. A module is an intermediate language (IL) file that does not have an assembly manifest.
assembly.dll can not be the source of Assembly Linker because it already is an assembly with manifest. So you get this
ALINK: warning AL1020: Ignoring included assembly 'assembly.dll'

Tesseract - Add reference does not works

I'm following the steps:
Download binary here, add a reference of the assembly Tessnet2.dll to your .NET project.
Download language data definition file here and put it in tessdata directory. Tessdata directory and your exe must be in the same directory.
Look at the Program.cs sample
if I call the class from lib using:
tessnet2.Tesseract ocr = new tessnet2.Tesseract();
I'm getting the following error:
Name Namespace or type 'tessnet2' could not be found.
Need a using directive or a set of reference
Modules (assembly)?
How I solve this?
Thanks!
There are two DLL versions for tessnet2: 32- and 64-bit. Make sure you add the correct one to your project. Check VietOCR.NET 2.0x for a working example of using tessnet2.
The error says that it can't find the namespace - even though you've added a reference to it, do you either name it using the full namespace, or can you include it as a "using" at the top of the cs file?
For reference, the translated error (from Portuguese) is:
Namespace or type 'tessnet2' could not be found. Need a using
directive or an assembly reference (assembly)?

compile cs files with mono?

I am trying to compile my project with mono on linux. My cmd looks something like...
gmcs Pages/UserProfile.cs Properties/AssemblyInfo.cs queues.cs watch_editor.cs Class1.cs -define:USE_SQLITE -r:System -r:System.Collections -r:System.Collections.Generic -r:System.Collections.ObjectModel -r:System.Collections.Specialized -r:System.Configuration
but much long. and i get the output
error CS0006: cannot find metadata file `System.Collections'
error CS0006: cannot find metadata file `System.Collections.Generic'
error CS0006: cannot find metadata file `System.Collections.ObjectModel'
...
How do i solve this?
I also tried it the other way around (below) and had the same error msg with .dll at the end of them
gmcs -define:USE_SQLITE -r:System.dll -r:System.Collections.dll -r:System.Web.UI.WebControls CommentCenter.cs cookies.cs db.cs Default.aspx.cs
You are confusing assemblies and namespaces. An assembly, such as System.dll, is a binary library file that can contain types in multiple namespaces. A namespace can be split across multiple assemblies, and the assembly name doesn't have to match the namespaces in any way.
-r is for referencing assemblies. You do not need to reference namespaces.
Given that most of the classes in those namespaces are in mscorlib (which is referenced by default) or System.dll, you probably just want
gmcs Pages/UserProfile.cs Properties/AssemblyInfo.cs queues.cs watch_editor.cs Class1.cs -define:USE_SQLITE -r:System
I strongly suggest you use an IDE such as MonoDevelop. You can view the compiler commands it generates, if you're interested.

Categories