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

Using DLL Component as Embedded Resource (Using Reflection)

, 20 Aug 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
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.Smile | :) 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)

Share

About the Author

FetalDump
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

 
QuestionHow to use a unmanged C++ dll Component( that doesn't require instantiation) as Embedded Resource using Reflection ? Pinmembermanutechalpy3088-Apr-09 19:35 
Generalvb.net PinmemberAngel T26-Jun-07 4:48 
GeneralRe: vb.net PinmemberGrant Porteous24-May-08 16:43 
QuestionError PinmemberMember #327494324-Feb-07 0:14 
GeneralcecUnamanaged dll as a resource Pinmemberraghab19-Feb-07 22:42 
Generalre Using DLL component as Embedded Resource (Using reflection) [modified] Pinmemberhaida18-Jan-07 4:40 
GeneralError loading project Pinmemberajay20059-Jan-07 4:05 
QuestionError loading project [modified] PinmemberNbranquinho18-Sep-06 4:20 
I downloaded this project to try understand how to use embedded resources but when I open the project give the error "Unable to read the project file 'TestVisual.csproj'. The file [...]\testvisual.csproj is not a valid project file. The project file is missing the 'VisualStudioproject' section".
 
Can someone help me? Sigh | :sigh:
(i'm using C# 2003)
Thanks
 

GeneralSerial Number Pinmemberregedit22-Aug-06 2:37 
GeneralRe: Serial Number PinmemberFetalDump22-Aug-06 4:21 
GeneralRe: Serial Number Pinmembercaiafaverde29-Aug-06 1:28 
GeneralILMerge Pinmembertonyt21-Aug-06 9:56 
GeneralRe: ILMerge PinmemberFetalDump22-Aug-06 4:25 
GeneralRe: ILMerge [modified] PinmemberBoris Boef29-Aug-06 0:56 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.141015.1 | Last Updated 21 Aug 2006
Article Copyright 2006 by FetalDump
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid