Showing posts with label .Net Assembly. Show all posts
Showing posts with label .Net Assembly. Show all posts
Wednesday, March 24, 2010
What is Reflection
Reflection means to find out metadata information of any modules or assembly. You can also create class using reflection.
Developer can load any assembly runtime and find out metadata information of that assembly. He can also create object of any type which are exist in that assembly and also able to assign value and call any method from that object at runtime.
Let’s take an example, you have to use any third party control (DLL). You can get your requested type dynamically.
Here I explained you how to load any assembly which is added as a reference in your project.
You are getting one Test Library1 (TestLibrary1.dll) and you have to find out “Class1” type. You have to add reference of that class library and call this code snippet to load library:
Assembly assembly = Assembly.Load("TestLibrary1");
Type t = assembly.GetType("TestLibrary1.Class1");
Here we have pass “TestLibrary1” as a parameter of Assembly.Load method is an assembly name. This is same as a namespace name.
Assembly is in-built .net class which has static method “Load” which load assembly so developer can easily find out types.
After loading assembly, you can find out your type using GetType method of assembly object.
Load method of Assembly class has different overloads. You can also pass physical path or Assembly name or byte array of assembly name.
You can download code from here.
Sunday, February 14, 2010
Register .net assembly as COM
Introduction
We have used VB6.0 DLL or OCX component in our .Net application. But sometime we need to use our .Net DLL into VB6.
In the following scenario you need to use .Net DLL into VB6.
- Any third party module is available in .Net and you need to integrate it with your VB6.0 application
- Your some modules are already developed into .Net and you have to integrate it with VB6.0 application
This blow will explain you how to create COM compatible .Net assembly step by step.
First we need to create one class library project. Open Visual Studio and create new Class Library project. Give “DemoClassLibrary1” name to your class library.
Create new class which needs to be expose as a COM object. Just add new class into your class library project. Give “DemoClass1” to your class name.
Public Class DemoClass1
Public Function GetWelcomeString(ByVal name As String) As String
GetWelcomeString = "Welcome " + name
End Function
End Class
Now to create it COM compatible, you have to add ComClass attribute on your class definition. So updated code will look like below:
Public Class DemoClass1
Public Function GetWelcomeString(ByVal name As String) As String
GetWelcomeString = "Welcome " + name
End Function
End Class
I have created one method which will be exposed as a COM method.
You also need to set your project as a Com compatible. To configure it, open project property. Right click on project in solution explorer and select properties option. In compile tab, select checkbox “Register for COM Interop”.
Now when you will build your project, project will create DLL as well as TLB file. This TLB file needs to use your .Net DLL into VB6.
Let’s use this DLL into VB6. Open your VB6 project and open reference dialog to add your .Net class reference. You will get option for “DemoApplication1”, select the option and click on ok.
Now you can create object of “DemoClass1” and use your function “GetWelcomeString”.
If you want to use this component on another machine then you have to copy the DLL file on that machine and register your DLL file to the registry so your VB6 application can find your component.
To register the component, Open Visual Studio 2008 command prompt and execute following command.
Regasm <<DLL file full path>> /tlb:<<tlbfilename>> /codebase
Regasm command generate TLB file and also register it with the system. Codebase option also set the code base with the registry.
Labels:
.Net Assembly,
.Net DLL,
COM,
ComClass,
Interop,
VB6.0,
Visual Basic 6.0
Subscribe to:
Posts (Atom)
