Introduction
Hello! First of all, I need to say some words about the main aim of this topic. This is Reflection. Reflection is a great instrument for a programmer. You can use it for dynamically creating an instance of some types, bind the type to an existing object or get the type of the existing object.
My sample application gets the main system info. You may create your own.:) Source code isn't quite good, but it's just an example of how you can use a DLL component from resources. Some time ago, I downloaded IrisSkin component to make my applications in skin automatically, including all forms and Windows. This component is quite good, but I had to distribute IrisSkin.DLL and other stuff with my application... and I really didn't want to do that...
So I decided to use IrisSkin component from applications resources using Reflection.
First of all, we should add IrisSkin.DLL to our solution and set as Embedded Resource BuildAction
for this component.
If we are using IrisSkin component like an Extern resource (referencing), InitializeComponent
method seems like this (in the general case):
private void InitializeComponent()
{
this.skinEngine1 = new Sunisoft.IrisSkin.SkinEngine(
((System.ComponentModel.Component)(this)));
this.SuspendLayout();
this.skinEngine1.SkinFile = null;
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 273);
this.Name = "Form1"; this.Text = "Form1";
this.ResumeLayout(false);
}
But we are using Iris Component like an Embedded Resource, so InitializeComponent
method in this case should seem like this (in the movement of my application):
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources =
new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.skinEngine1 = GetDLLComponent();
this.SuspendLayout();
...
}
GetDLLComponent
is the heart of this block of code. Let's view this part of code. Here we go...
public object GetDLLComponent()
{
System.Reflection.BindingFlags flags =
(System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Public |
System.Reflection.BindingFlags.Static |
System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.DeclaredOnly);
System.Globalization.CultureInfo clinf = new System.Globalization.CultureInfo(
System.Globalization.CultureInfo.CurrentCulture.Name);
System.Reflection.Assembly assembly = System.AppDomain.CurrentDomain.Load(
GetEmbededResource("testVisual.IrisSkin2.dll"));
supaObj = assembly.CreateInstance("Sunisoft.IrisSkin.SkinEngine",
false, flags, null,
new object[] { ((System.ComponentModel.Component)(this)) },
clinf, new object[] { });
System.Type supaType = supaObj.GetType();
System.ComponentModel.ComponentResourceManager resources = new
System.ComponentModel.ComponentResourceManager(typeof(Form1));
((supaType.GetProperty("SerialNumber")).GetSetMethod(true)).Invoke(
supaObj, new object[]
{ "isBx30VBCC0GTnUjKOl7Q0jK7I8NqeyOjDigXGIG5v1dN7aw4qc3Ogw==" });
((supaType.GetProperty("SkinFile")).GetSetMethod(true)).Invoke(supaObj,
new object[] { ((System.IO.Stream)(
resources.GetObject("DiamondBlue.ssk"))) });
((supaType.GetProperty("SkinStreamMain")).GetSetMethod(true)).Invoke(supaObj,
new object[] { ((System.IO.Stream)(
resources.GetObject("skinEngine1.SkinStreamMain"))) });
return supaObj;
}
So, if you are acquainted with Reflection and OOP;), this part of code will be quite simple for you. If you have any suggestions or questions - you are welcome! I hope this code will help you.
History
- 20th August, 2006: Initial post