Click here to Skip to main content
15,881,172 members
Articles / Programming Languages / C#
Article

Using DLL Component as Embedded Resource (Using Reflection)

Rate me:
Please Sign up or sign in to vote.
4.36/5 (10 votes)
20 Aug 2006CPOL1 min read 92.1K   3.5K   45   14
Describes how you can use DLL component without reference from resources
Sample screenshot

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):

C#
private void InitializeComponent()
{
   this.skinEngine1 = new Sunisoft.IrisSkin.SkinEngine(
                      ((System.ComponentModel.Component)(this))); 
    this.SuspendLayout(); 
    // 
    // skinEngine1 
    // this.skinEngine1.SerialNumber = ""; 
    this.skinEngine1.SkinFile = null; 
    // 
    // Form1 
    // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
    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):

C#
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...

C#
public object GetDLLComponent()
{
// BINDING FLAGS
System.Reflection.BindingFlags flags = 
     (System.Reflection.BindingFlags.NonPublic | 
      System.Reflection.BindingFlags.Public |
      System.Reflection.BindingFlags.Static | 
      System.Reflection.BindingFlags.Instance | 
      System.Reflection.BindingFlags.DeclaredOnly);

// CULTURE INFO
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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Russian Federation Russian Federation
FetalDump has been developing about 6 years.
He is a lead developer of DataFort Company(Ibs Holding http://www.ibs.ru)
He lives in Moscow.
His interests are: Music and Painting.

Comments and Discussions

 
Generalre Using DLL component as Embedded Resource (Using reflection) [modified] Pin
haida18-Jan-07 4:40
haida18-Jan-07 4:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.