My application references another a project which has an XSD file in it.
Whats the best way to get that XSD?
I did a bit of googling and found suggestions like load the assembly and get it from that, is there no easier way?
If the XSD is an embedded resource in the assembly, then you need to get it from the assembly.
If your project references and uses the assembly, then you won't need to load it again (you don't need 2 copies in memory).
The easiest way to get to the assembly, would be from one of the types defined in it:
Type t = typeof(TypeInOtherAssembly);
Assembly assembly = t.Assembly;
assembly.GetManifestResourceStream(...);
If you've added the XSD as a resource then the easiest way is to make the auto-generated Properties.Resources class publicly visible and reference the auto-generated property. You could also keep Properties.Resources internal and add an InternalsVisibleTo attribute to allow your other assembly to have access.
Other than that approach, you can use the GetManifestResourceStream on the target assembly to extract the XSD information.
Related
I need to add reference to another assembly in my c# project based on some compiler switch like #ifdirective. For example I want to add reference to logger DLL in my project only when I need it. Is this possible?
As far as I know, a referenced assembly that isn't used AT ALL, isn't a problem. You can even keep it as reference.
As long as your code doesn't trigger the assembly to be loaded, there is no need to have that file available.
I would suggest though to check whether you really need this, and if you can workaround this by creating interfaces and dynamically load the assembly (using Assembly.LoadFrom).
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 have a current assembly in my application and I would like to add a class from external cs file into this assembly. Is it possible to do it? I would like to use it like plug-ins. Now I'm trying use:
System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
and Activator,but without success. My application is using Roslyn, so maybe it can do it.
Any idea will be appreciated.
Edit: Next problem with it is: Can I use external file (cs file with class) to get instance from this file but the constructor of class needs reference to sceneManager from current assembley. So is possible to send a reference to Roslyn of something like that and get instance of class from it?
You cannot modify an existing assembly that has already been loaded.
Instead, you can compile code to a new assembly (using Roslyn, CodeDOM, Sigil, or similar libraries) and load that assembly using reflection.
A '.cs' file by itself is just text. You can't do anything with it without compiling it through some route. But no: you can't add extra classes into an assembly at runtime. You can compile the code at runtime via CSharpCodeProvider or similar, and load the generated assembly. It is a lot of messing, though. Depending on the context, tools like Iron Python may be preferable, if you need to do a lot of things from scripts at runtime.
I have one assembly that's created already, say Static.dll, and a dynamic assembly I'm creating at runtime, say Dynamic. Dynamic creates some IL code to create a new type from Static.dll. When Dynamic is created (it saves successfully), the executable, when ran, errors out because it's unable to load the assembly Static.dll type. I believe this is happening since the Dynamic exe output can't find this dll (or at least this is my guess).
How can accomplish adding in this Static.dll reference so that when someone runs the resulting executable output of Dynamic that it can find the referenced types?
I know about Assembly.Load(), but this alone doesn't change anything. Or at least, I'm not sure what this gives me since I don't need to use the type at runtime, but rather when running the Dynamic executable.
Lastly, I control the Static.dll, so if there is something I can do with the project to fix it, It could be done, however, it does need to remain separate (I don't want to Emit this library for every time I create a dynamic assembly). Basically, I think I want to have my dynamic assembly to have a .assembly extern mscorlib in it's manifest save Static is place of the mscorlib.
Thanks
How can accomplish adding in this Static.dll reference so that when someone runs the resulting executable output of Dynamic that it can find the referenced types?
The reference will be added automatically. Note that normal assembly-probing rules apply when your dynamic assembly executes, so in order to find the assembly. You must (one of):
ship static.dll alongside dynamic.dll
put static.dll in the GAC (but please: don't do this!)
run dynamic.dll in a custom AppDomain with a custom probing-path configuration that lets static.dll be found
handle AppDomain.AssemblyResolve to provide static.dll
use something like ILMerge to fuse static.dll and dynamic.dll
Hi I do not know if this is possible or not but I have a c# Project lets say A and I am trying to access Assembly Info of another project B so that i can get Method Info of project B using Reflection. Problem is that i can not think of a way to integrate those two. Project A provides a openFileDialogue and it selects .csproj file. Reads it and extracts what files are being used in project B.
Can you suggest me a work out?
I don't think you can do that by using reflection. To work with reflection you'll need an assembly, not csproj (or cs files). You should look for a parser, maybe use the Roslyn APIs, that will give you information about the source code in syntax tree format.
http://blogs.msdn.com/b/visualstudio/archive/2011/10/19/introducing-the-microsoft-roslyn-ctp.aspx
Each .csproj file is XML, so you can read that in pretty easily. Listed in that file is every file included in the project, so you can parse the XML .csproj file to find all the .cs files.
From there, if you need to extract MethodInfo, you would have to either parse the .cs files, or use something like Roslyn to parse the code into its syntax tree, and find the methods that way.
Can you just use the built assembly (.exe or .dll) from "Project B" instead of its .csproj file? It would be a lot easier to load the assembly's reflection info, and just loop over every class and evey method...
Use Assembly.LoadFile to load directly the compiled assembly - i.e. the DLL or EXE; this will give you an Assembly object on which you can call GetTypes() etc. to access all the info you want.