Click here to Skip to main content
Click here to Skip to main content

Using DLL Component as Embedded Resource (Using Reflection)

By , 20 Aug 2006
 
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):

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

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()
{
// 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)

About the Author

FetalDump
Web Developer
Russian Federation Russian Federation
Member
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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionHow to use a unmanged C++ dll Component( that doesn't require instantiation) as Embedded Resource using Reflection ?membermanutechalpy3088 Apr '09 - 19:35 
Generalvb.netmemberAngel T26 Jun '07 - 4:48 
GeneralRe: vb.netmemberGrant Porteous24 May '08 - 16:43 
QuestionErrormemberMember #327494324 Feb '07 - 0:14 
GeneralcecUnamanaged dll as a resourcememberraghab19 Feb '07 - 22:42 
Generalre Using DLL component as Embedded Resource (Using reflection) [modified]memberhaida18 Jan '07 - 4:40 
GeneralError loading projectmemberajay20059 Jan '07 - 4:05 
QuestionError loading project [modified]memberNbranquinho18 Sep '06 - 4:20 
GeneralSerial Numbermemberregedit22 Aug '06 - 2:37 
GeneralRe: Serial NumbermemberFetalDump22 Aug '06 - 4:21 
Only shi... OMG | :OMG: Confused | :confused: WTF | :WTF: Unsure | :~
 
Just FetalDump...

GeneralRe: Serial Numbermembercaiafaverde29 Aug '06 - 1:28 
GeneralILMergemembertonyt21 Aug '06 - 9:56 
GeneralRe: ILMergememberFetalDump22 Aug '06 - 4:25 
GeneralRe: ILMerge [modified]memberBoris Boef29 Aug '06 - 0:56 

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 21 Aug 2006
Article Copyright 2006 by FetalDump
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid