I see in the CSharpInteractive.rsp you can add a reference to a DLL in the GAC using /r:
How do you add references to your own DLLs?
I tried System.Reflection.Assembly.LoadFrom, it didn't fail but didn't work.
I am trying to add a reference to my DLL that has extension methods.
If I try to add the code for the extension method directly in the interactive window I get this error:
error CS1109: Extension methods must be defined in a top level static class; XYZ is a nested class
You should be able to specify a full path for the assembly, in the same way as the gac assembly.
Normally you don't need to change the rsp though. You can add references in a regular submission using:
#r "path"
Disclaimer: I work at Microsoft on the Roslyn team.
Related
I am using CSharpCompilation.Create to create assembly from scratch. The thing I got to know is this always creates a new assembly. Is there anyway to add/modify the existing DLL in Roslyn? This is because I am building a tool where a user can type C# code in richtextbox and compile it to add it to the existing assembly. Is it possible, if not what I am doing wrong and what is the right way?
P.S.,
I have also tried CSharpCodeProvider as well to compile the code, but it is also Generating assembly with only the code I typed in richtextbox. The output assembly did not have other classes in that.
Yes as mentioned in comment by Klauss Gutter, you have to build the assembly individually and merge it later using ILMerge. I know ILMerge is out of support and is deprecated. You can use ILRepack to do this. It provides the same functionality as ILMerge.
You can use NuGet Package to install ILRepack first and then add reference to the ILRepack.exe to your project to use its methods to pack multiple assemblies in to one.
Roslyn or CSharpProvider wont help you to merge your assemblies. The main motto of these are to compile and create an assembly not merge.
Let's say you have two projects:
Main.csproj
Main.Test.csproj
And Main has an attribute
[assembly:InternalsVisibleTo("Main.Test")]
Now we want to reference a NuGet package that ships its content as source code such as MoreLinq (https://code.google.com/p/morelinq/) in both projects.
The problem is that there are now two copies of the source visible to the Main.Test project and therefore the compiler spews an Ambiguous reference error.
Is there any way to avoid this without modifying the source code that the package drops into each project?
P.S. Please don't use arguments such as 'don't use InternalsVisibleTo because...'. I can read those points elsewhere.
The extension methods from morelinq will be public so if you have them in main.csproj, you won't need them in main.test.csproj but they'll still be accessible within main.test.csproj.
From a solution, which I made in Visual Studio 2013, I want to transfer a method to a DLL (class library project).
When I paste the method in the DLL project, it doesn't recognize parts of the code and it's showing this error`:
"are you missing a using directive or an assembly reference?"
Because of that, the DLL can't be built. I am assuming, that a certain reference to the solution is required, but I am not sure how to proceed.
I know how to add a reference from a solution to a DLL, but I'm not sure how it's done the other way around or even if it's possible.
You can't add reference to EXE from class library (assuming EXE uses that class library) as it will introduce circular reference.
Usually you need to refactor all dependencies so class library either have them all or allow application to inject dependencies from EXE/other clients. In later case class library needs to define base classes/interfaces to allow such injection.
Yes, you need to restore the same references that the original project uses, if they are used in the code you want to move.
If you need to do this by hand (i.e. without tools like ReSharper):
Move the code to the new assembly.
For each namespace or type giving the error, find it in the Object Browser.
Locate the assembly containing that namespace and type, and add a reference to that assembly in your new project.
You may also have to add a Project Reference to the original project.
I found source of WPF.MDI and I try to use it in my project without any specific dlls. So, I added source into the project
Then, wrote xmlns declaration xmlns:mdi="clr-namespace:WPF.MDI" and created a container with MdiChild
<mdi:MdiContainer Theme="Aero">
<mdi:MdiChild />
</mdi:MdiContainer>
But, I received an error XamlParseException.
I guess, I do it wrong at all. But, there is any solutions for this?
UPDATE:
When I put WPF.MDI.dll into folder with .exe file and start it - there is no errors. Why it wants to find dll?
Try setting your namespace declaration like this:
xmlns:mdi="clr-namespace:WPF.MDI;assembly=MDISource"
So add the assembly part.
You need to add this if your class is in a different assembly. See MSDN:
assembly= The assembly that contains some or all of the referenced CLR
namespace. This value is typically just the name of the assembly, not
the path, and does not include the extension (such as .dll or .exe).
The path to that assembly must be established as a project reference
in the project file that contains the XAML you are trying to map. In
order to incorporate versioning and strong-name signing, the assembly
value can be a string as defined by AssemblyName, rather than the
simple string name.
assembly can be omitted if the clr-namespace referenced is being defined within the same assembly as the application code that is referencing the custom classes. Or, an equivalent syntax for this case is to specify assembly=, with no string token following the equals sign.
UPDATE
Read this answer.
The problem was solved after deleting the strings 96-100 in MdiContainer.cs
if (Environment.OSVersion.Version.Major == 5)
ThemeValueChanged(this, new DependencyPropertyChangedEventArgs(ThemeProperty, Theme, ThemeType.Luna));
else
ThemeValueChanged(this, new DependencyPropertyChangedEventArgs(ThemeProperty, Theme, ThemeType.Aero));
I am working on an assignment that specified "Do not use any external libraries". So I created a c# application, and the first thing I did was remove all the dll's references by default... including "System.dll".
However, I can still add this to my code:
using System;
using System.IO;
I was just curious as to how come I do not need to have System.dll as a reference in my project to do this. Thanks!
mscorlib.dll includes items in both those namespaces.
You need to right-click your project > Properties > Build > Advanced... and check "Do not reference mscorlib.dll" to remove this reference.
Different assemblies can contribute to the same namespace.
Even if you don't reference System.dll, you are still referencing (implicitly) mscorlib.dll which contributes many types to the System namespace.
These references are probably defined in your Web.config or the Machine.config file so they're included by default.
These are the default libraries.I think your question is that "Dont use third party dlls"
Another thing to consider is, if you're compiling directly through the command line, a default set of switches, including default library references, is parsed by the compiler through the default response file (csc.rsp), located in the same directory as the compiler. The fact that you are able to import namespaces from the Base Class Library without explicitly referencing them at compile time is due to the fact that their containing assemblies are included in your program by default. To change this behavior at the command line, you can use the /nostdlib switch to force it not to include mscorlib.dll, or you can use /noconfig to have it ignore the entire default response file altogether. Also, I'm not too sure what you mean by system.dll, because the namespaces you mentioned are contained within mscorlib.dll. Also, I think by "external library", your instructor must have meant any 3rd party assemblies that would assist you in solving the problem. Anything that comes included with the .NET SDK would be more of a framework library. Unless your teacher is really harsh and wants you to reinvent the wheel :P