I need to resolve an assembly and type at runtime and I need to find the fully qualified type name. For some reason I cannot get it right, as I keep get an exception saying it cannot find the type specified.
The app.config file, in which the assembly to look for is defined, looks like this:
<configSections>
<section name="modules" type="Microsoft.Practices.Composite.Modularity.ModulesConfigurationSection, Microsoft.Practices.Composite"/>
</configSections>
<modules>
<module assemblyFile="G:\Data\Visual Studio 2008\Projects\Race Management System.Shell\ConfigurationModularity\bin\Debug\Modules\Driver.Data.Module.dll" moduleType="Driver.Data.Module.DriverDataModule, DriverDataModule" moduleName="Driver.Data.Module.DriverDataModule"></module>
</modules>
The assembly is called: Driver.Data.Module
Namespace in assembly is: Driver.Data.Module
and type name is: DriverDataModule, and this is also the name of the .cs file.
I cannot seem to find how to specify the name correctly in the xml file. Can somebody help me with the fully qualified type name?
This is for a Composite WPF application.
Thanx!
Try Driver.Data.Module.DriverDataModule, Driver.Data.Module.
You can also find the full assembly-qualified name of your type by instantiating an object of that type and examining the AssemblyQualifiedName property of its Type:
DriverDataModule module = new DriverDataModule();
string fullyQualifiedName = module.GetType().AssemblyQualifiedName;
What error do you get? If you're having a hard time getting a full error message out of the app, and you think your app is having problems loading the assembly itself, you can use the fuslogvw tool to log full details to disk.
It's worth also opening the assembly in Reflector to double-check the assembly's full name (displayed on the bottom-left of the window when you open Reflector), and to check that the type is in fact defined in the namespace you think it is.
Related
Say I'm working inside a namespace called, for example, Org.Company and that this namespace contains MyClass.
I'm also importing a nuget with a namespace called Company with a class named OtherClass.
So, when I'm working inside my Org.Company namespace, I can't do the following:
Company.OtherClass obj;
because the compiler assumes that I actually mean:
Org.Company.Otherclass obj
which doesn't exist.
So, as far as I know, instead of using the fully qualified name, I actually MUST import the other namespace as such using Company;
The problem, however, is that I need to reference this OtherClass from a XML file (Castle Windsor configuration file) and the fully qualified name Company.OtherClass isn't working.
Is there a way around this? Changing namespace names isn't a viable option.
EDIT:
This is what I have on my Castle Windsor xml file
<using assembly="MyProj, Version=1.0.0.0, Culture=neutral" />
...
<component
service="Company.IOtherClass"
type="Company.OtherClass"
/>
I get the following error:
{"Could not convert string 'Company.OtherClass' to a type. Make sure assembly containing the type has been loaded into the process, or consider specifying assembly qualified name of the type."}
Probably because its looking inside Org.Company instead, defined in the MyProj assembly.
I assume this could be fixed if there was a way to add another <using /> statement referencing the Nuget package... Is there a way to do that?
As far as I understand your problem is about referencing from XML? Right?
So as Castle.Windsor suggests "consider specifying assembly qualified name of the type.". What does it mean? It means that instead of:
<component
service="Company.IOtherClass"
type="Company.OtherClass"
/>
You should give class names with assembly they are coming from:
<component
service="Company.IOtherClass, OtherClassAssembly"
type="Company.OtherClass, OtherClassAssembly"
/>
or even fully qualified assembly name:
<component
service="Company.IOtherClass, OtherClassAssembly, Version=..., Culture=..., PublicKeyToken=..."
type="Company.OtherClass, OtherClassAssembly, Version=..., Culture=..., PublicKeyToken=..."
/>
NOTE: replace OtherClassAssembly with actual assembly name and DON'T add .dll extension here
NOTE: see http://msdn.microsoft.com/en-us/library/system.type.assemblyqualifiedname.aspx for more details, for example how to deal with nested classes
To use OtherClass in code, well, you can use global
global::Company.OtherClass
you can also use using:
using Company;
or just import one class:
using ClassFromOtherCompany = Company.OtherClass;
You can use namespace alias, something like this MSDN example:
using Co = Company.Proj.Nested;
Well, judging from the description of the problem and the error message you're getting it would appear your suspicion is correct.
The assembly from Nuget package hasn't been loaded into the AppDomain by the time Windsor runs its installers, so it has no way of knowing where to get the Company.OtherClass from.
You can then either do as the error message suggests, and use assembly qualified type name, or preload the assembly, with <using /> tag.
I'm using oracle coherence in my C# project.
.Net project has references to Coherence and it sets some Coherence properties by 'injecting' my types. One of the examples if POF configuration:
<user-type>
<type-id>1008</type-id>
<class-name>MyTypeName, MyAssembly, version=1.2.3.4, publicKeyToken=0f73b23f05811dc2</class-name>
</user-type>
Even though all my types are specified by using full name with version and public key token, Coherence doesn't use it and binds to MyAssembly.
It's a big problem for me bacause MyAssembly is in GAC and binding fails. Of course I can use an application config and set:
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<qualifyAssembly partialName="MyAssembly" fullName="MyAssembly,Version=1.2.3.4,publicKeyToken=0f73b23f05811dc2,culture=neutral" />
</assemblyBinding>
But I would like to avoid that (not every application can have a app.config - like PowerShell).
In Oracle documentation http://docs.oracle.com/cd/E18686_01/coh.37/e18678/net_intobjects.htm#BABJCBDD I found that:
You need not specify a fully qualified type name within the class-name
element. The type and assembly name is enough.
But what if I want to specify fully qualified name?
--Edit
Right new the issue I'm having is with the tangosol-coherence.override.xml:
<security-config>
<identity-transformer>
<class-name>MyAssembly.IdentityTransformer, MyAssembly, Version=1.2.3.4, publicKeyToken=0f73b23f05811dc2, culture=neutral</class-name>
</identity-transformer>
<principal-scope>false</principal-scope>
</security-config>
the exception I get:
Failed to instantiate class
"MyAssembly.IdentityTransformer,
MyAssembly, Version=1.2.3.4,
publicKeyToken=0f73b23f05811dc2, culture=neutral"
MyAssembly.IdentityTransformer,
MyAssembly, Version=1.2.3.4,
publicKeyToken=0f73b23f05811dc2, culture=neutral
in Assembly Binding Log Viewer:
WRN: Partial binding information was supplied for an assembly: WRN:
Assembly Name: MyAssembly, Version=1.2.3.4 | Domain ID: 1
Calling assembly : Coherence, Version=3.7.1.3, Culture=neutral, PublicKeyToken=0ada89708fdf1f9a.
I solved that issue by analysing Coherence disassembled dll.
It looks like:
Tangosol.Util.TypeResolver.Resolve(typeName);
cannot resolve type when it specified like this:
MyAssembly.IdentityTransformer, MyAssembly, Version=1.2.3.4, publicKeyToken=0f73b23f05811dc2, culture=neutral
but it can when it's specified like this:
MyAssembly.IdentityTransformer, MyAssembly, Version=1.2.3.4, Culture=neutral, PublicKeyToken=0f73b23f05811dc2
So modyfing the tangosol-coherence.override.xml fixed it.
I'm trying to include a html to pdf converter in my C# program for windows mobile.
I found Pdfizer.
I added a refence to Pdfizer.dll and included it with using Pdfizer;
Also I added a reference to itextsharp that is required for Pdfizer.
Pdfizer is correctly added and I can use many of its functions and classes but I can't use HtmlToPdfConverter.Run()
My code:
FileStream filePDF;
filePDF = File.Create(path + ".pdf"); // path is string
html2pdf.Open(filePDF);
html2pdf.AddChapter(#"Chapter name");
html2pdf.Run(html); // html is a string that contains html code
html2pdf.Close();
Problem is in line html2pdf.Run(html); It says:
Error 1 The type 'System.Uri' is defined in an assembly that is not referenced.
You must add a reference to assembly 'System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
Definition of Run is
public void Run(string html);
public void Run(Uri uri);
I'm trying to use the Run(string html) option, but Uri is defined and visual studio says it is not referenced but yes it is.
I have using System; that contains Uri class, indeed I use it in other functions and works properly.
What is the problem? Should I add a reference to System, Version=1.0.5000.0? Where can I find It? Is there any way to tell Pdfizer Uri is in System.Uri?
Thanks
A public key token of b77a5c561934e089 indicates that you're referencing a desktop assembly, so my bet is that Pdfizer is built only for the desktop. There is no way to make that work in the COmpact Framework. If you can get the source, you can try to compile it for the CF, otehrwise you'll have to ask the owners if they have a CF version.
I have a project named "Test.LiveModel" (Test.LiveModel.dll) and its version is 8.0.7.0 in my solution which contains 25 projects. I can see the information of Test.LiveModel in AssemblyInfo.cs. I have two category of objects named 'base class category' and 'user-defined class category' which are displaying in my application UI. I am displaying this through a property which is of class Type
Now I am considering one base class category object named "Server" and one user-defined class category object RoundedTree. When I set value as "Server" in Property in Grid after saving it when I restart my application I can see the saved value, but for "RoundedTree" which is not happening due to type becomes null. So I did a thorough analysis and came to know that issue is in ToType() method shown below
This is ToType() metho
For base class Server xmlSerializableType.Name, I am getting as Test.LiveModel.Server and AssemblyName I am getting as Test.LiveModel, Version=8.0.7.0, Culture=neutral, PublicKeyToken=23bd062a94e26d58 and type I am getting by using Type.GetType as type = {Name = "Server" FullName = "Test.LiveModel.Server"}
But for user defined class xmlSerializableType.Name I am getting as _Rounded_Tree. 'type' I am getting as null by using Type.GetType. AssemblyName I am getting as _Rounded_TreeTest-Machine, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null, but even assembly.GetType I am getting as null. What is the reason behind it? Why am I getting assembly version 0.0.0.0? I mean full assembly _Rounded_TreeTest-Machine, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null.
This is the method CreateType() which will create assembly and type as myTypeBuilder for userdefined class:
public Type CreateType()
{
// Create the assembly name by appending the machine name to the typename.
myAsmName.Name = this.TypeName + Environment.MachineName;
// Define assembly that can be executed but not saved
this.UserClassAssemblyBuilder = myDomain.DefineDynamicAssembly(myAsmName, AssemblyBuilderAccess.Run);
// Create dynamic module with symbol information
this.UserClassModuleBuilder = this.UserClassAssemblyBuilder.DefineDynamicModule("userdefinedmodule", true);
So here is my question: if real Dll has some version number, and user defined class assembly has version 0.0.0.0, is that the reason why I am getting type as null after using Type.GetType and assembly.GetType method?
Here are some suggestions which may solve the problems.
Define a assembly version
new AssemblyName(this.TypeName + Environment.MachineName)
{
Version = new Version("1.0.0.0")
};
Use full qualified names for the serialization
myObject.GetType().FullName
Even though the solution is so obvious I should have never have posted this, I'm leaving it up as a reminder and a useful point of reference to others.
I've got the following in my app.config file:
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
</sectionGroup>
Followed by:
<spring>
<context>
<resource uri="config://spring/objects"/>
</context>
<objects xmlns="http://www.springframework.net">
<object name="mediaLibrary" type="AlbumLibraryWPF.AlbumLibrary, AlbumLibraryWPF"/>
</objects>
</spring>
Then in my app I've got:
using Spring.Context;
using Spring.Context.Support;
public partial class AlbumChecker : Window
{
private DataTable dataTable;
private Library library;
private Thread libraryThread;
public AlbumChecker()
{
InitializeComponent();
CreateToolTips();
IApplicationContext ctx = ContextRegistry.GetContext();
library = (Library)ctx.GetObject("mediaLibrary");
// Other initialisation
}
// Other code
}
It all compiles quite nicely, however, I'm getting an exception raised on the call to GetContext():
Error creating context 'spring.root': Could not load type from string value
'AlbumLibraryWPF.AlbumLibrary, AlbumLibraryWPF'.
I've checked the Spring.NET documentation and can't see what I'm doing wrong - but I clearly have got something wrong, otherwise it wouldn't raise the exception!
AlbumLibraryWPF is the namespace and AlbumLibraryWPF.AlbumLibrary is the fully qualified name of the class I want to instantiate. I'm guessing that it's this I've got wrong, but can't see how.
I feel such a fool.
It was because I'd failed to copy the AlbumLibrary.dll to the correct output directory. That meant that Spring couldn't find it - even after I'd fixed the assembly name problem Kent highlighted.
The name after the comma should be the assembly name, which is not necessarily the same as the namespace name.
I was getting this error because by mistake there was a typo [!*2] in app.config file. Once I took that out , error went away. some thing like this
<context>
<!--<resource uri="~//Aspects.xml"/>-->
<!--<resource uri="~//Dao.xml"/>-->
<!--<resource uri="~//Spring.xml"/>-->
<resource uri="file://Spring.xml"/>
<resource uri="file://Dao.xml"/>
</context>
!*2
You should use tha id attribute instead of name:
<object id="mediaLibrary" type="AlbumLibraryWPF.AlbumLibrary, AlbumLibraryWPF"/>
Also it should be config://spring/objects instead of config://spring/obects.
You need to double check that you have a type called AlbumLibrary in AlbumLibraryWPF namespace defined in AlbumLibraryWPF assembly.
You can try change the type. The type="AlbumLibraryWPF.AlbumLibrary, AlbumLibraryWPF", first parameter means NameSpace and the second parameter (behind the dot) means Solution Name.
"AlbumLibraryWPF.AlbumLibrary" = NameSapce name
"AlbumLibraryWPF" = solution name
Open VS2012 or VS2010 with Administrator Permissions
Config: type="namespace.type, assembly"
Then try running your solution again.