Click here to Skip to main content
15,915,164 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,

I want to have a generic version of this code :
OdB.dll contains fenetre_OdB.cs with :

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using ComponentFactory.Krypton.Ribbon;
namespace OdB
{
    public partial class fenetre_OdB : UserControl
    {
        public KryptonRibbonTab _monRuban = new KryptonRibbonTab();
                
        
        public KryptonRibbonTab monRuban
        {
            get { 
                return _monRuban; }
        }
        public fenetre_OdB()
        {
            _monRuban.Text = "toto le hero"; 
            InitializeComponent();
        }


    }
}

and

Main project has a reference to OdB.dll and contains this code to get _monRuban value :
OdB.fenetre_OdB toto = new OdB.fenetre_OdB();
KryptonRibbonTab t = toto._monRuban;
MessageBox.Show(t.Text);


I want to get _monRuban with a generic solution. First
Assembly MonAssembly = Assembly.LoadFrom("OdB.dll");


I can change everything (abstract for example)

Can you help me please ?
Posted

Lets say you have a Class library called MyClassLibrary, and you want an application called MyApplication to run a method on a object in MyClassLibrary without adding a project reference that you can do something like this:

MyClassLibrary:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyClassLibrary
{
    public class MyClass
    {
        public MyClass()
        {
        }

        public string SomeValue
        {
            get { return "Setec astronomy"; }
        }
    }
}


MyApplication:
C#
namespace MyApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            Assembly assembly = Assembly.LoadFile(@"C:\Temp\MyClassLibrary\MyClassLibrary\bin\Debug\MyClassLibrary.dll");

            Type type = assembly.GetType("MyClass");
            object instance = type.GetConstructor(Type.EmptyTypes).Invoke(new object[0]);

            string theValue = type.GetProperty("SomeValue").GetValue(instance, null);
        }
    }
}


This would load the assembly at runtime using a path rather than a project reference and the use reflection to find and invoke the constructor and the property.

Hope this helps.
 
Share this answer
 
Comments
Kishor Deshpande 3-Feb-13 21:43pm    
My 5.
Using the name of the class to find its definition in some assembly is a really, really bad idea. Think about it: should you misspell it, the compiler won't give you a clue. Such method is still needed, because it is used in serialization. But serialization is a very special thing: it uses not just System.Reflection, but, importantly, System.Reflection.Emit, which is critical for performance.

You use Reflection when you want to abstract out some implementation and provide different alternative implementations loaded during run time. If you think about it, you will see that you never need to know the names of the types. If you need some different implementations, ask yourself: implementation of what? Apparently, it can be expressed as implementation of some interface (interfaces). So, write some interface explicitly and find its implementation is dynamically loaded assemblies. I explained it in my past answers:

Gathering types from assemblies by it's string representation[^],
Create WPF Application that uses Reloadable Plugins...[^],
AppDomain refuses to load an assembly[^].

—SA
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900