There are lot of new things in VS 2005( aka .NET 2.0 ), I was doing some research on the referencing, versioning assemblies and I found there are lot of new features... I want to talk about two striking features on this ground ....
1) Adding an exe as reference: In .NET 1.1 we could only add reference of Library assembly( dll ), so we had to make sure that all the sharing code should be in a dll. .NET 2.2 allows us to add
a) Library assembly( dll ) and also b) Application assembly( exe ). So, now we can add any exe as a reference to our assembly and we can access all the public class::public methods/properties of that assembly, and another important thing is that the referenced exe will also run in the same app domain of the application( the application which has added this reference ).
I think this feature can be useful at times, but at the same time we should use Library assembly to share codebase.( the old and gold way -:) )
2) Resolving type name conflicts in a new way( new feature 'alias' ): The second feature is more interesting ... consider the following case ...
we are writing an application which will add two third party dll, say name of those dlls are ThirdParty1.dll( namespace is ThirdParty1) and ThirdParty2.dll( namespace is ThirdParty2) and both the dll has a public class named Customer. In our win application( in a class ) we have added both the namespace in the Using directive.
now if we write something like this
Customer c = new Customer();
Compiler will raise an error since Customer is defined in both ThirdParty1.dll::ThirdParty1 namespace and ThirdParty2.dll::ThirdParty2 namespace, so it could not resolve the class definition since it is ambiguous.
To fix this problem we should rewrite our code as ...........
ThirdParty1.Customer c = new ThirdParty1.Customer();<>
The above behavior is same in .NET 1.1 and .NET 2.0.
Now, let us make the situation little more complex .. how if both the dlls have the same namespace say both the dlls have "ThirdParty" as namespace.
Now in the application if we write ..... Customer c = new Customer();
In .NET 1.1 there should not be any error, and we can see in the IDE( VS 2003 ) that whole line is underlined with blue color, and it says that the Customer class is defined in multiple places and it is taking the definition from one of them.
I found that it takes the "Customer" definition by the order of assembly adding, like if we add ThidParty1 first then it will be using ThirdParty1::Customer.( does anybody has different idea )
In .NET 2.0 the code< c =" new"> will raise an error, it does not follow the path of .NET 1.1, so how to solve this issue ? To solve this issue we have to do the following ...
a) In VS 2005 every reference has an alias( new feature in 2.0 ) and by default the alias is "Global" so first we have to change the alias of one the referenced assembly, say let us change the alias of ThidParty1 as "MyThidParty".( Select the reference from the solution explorer and click F4 to see the properties of the assembly and you can find the alias here )
b) We need to add the following line [ extern alias MyThidParty;] before writing any using directive, .NET 2.0 compiler always use "Global" alias, not any other alias, so to use any other assembly which has different alias we need tell the compiler using the above statement.
Now we are all done ..
Customer c = new Customer(); will use the definition from ThirdParty2 assembly( the global aliased one ). To create an instance of "Customer" class which is defined in the ThirdParty1 we have to create the instance in this manner ----
MyThirdParty::ThirdParty.Customer c = new MyThirdParty::ThirdParty.Customer(); [aliasname::namespace.classname ]
Saturday, September 16, 2006
Subscribe to:
Posts (Atom)