Hello,

I created a dynamic object but I do not know how to use it when the instanciation of the object is finished.
I would like to call some methodes from Class Base by an objet that I created dynamically. Can you explain me, pls? Thank you

This is my code:
MAIN

static void Main(string[] args)
    {
                /*
                 * Electric
                 * Hydraulic
                 * Mechanic
                 */
                Object objObject;
                Tools objTool = new Tools();
                objObject = objTool.CreateInstance("Electric");

                try
                {
                   //objObject. ??? (how to call some methodes from Class Base ?)
                }
                catch(Exception)
                {
                }

                Console.Read();
    }

Class Base

class Base
    {
            public void setStarted()
            {
                Console.WriteLine("Started");
            }
            public void setStopped()
            {
                Console.WriteLine("Stopped");
            }
            public void setStoppedUrgently()
            {
                Console.WriteLine("Stopped Urgently");
            }

    }

Class Tools

class Tools:Base
    {
            public object CreateInstance(string sTypeName)
            {
                foreach (Assembly objAssembly in AppDomain.CurrentDomain.GetAssemblies())
                    foreach (Type objType in objAssembly.GetTypes())
                        if ((sTypeName == objType.Name) || (sTypeName == objType.FullName))
                        {
                            return Activator.CreateInstance(objType);
                        }
                return null;
            }
    }

You could use e.g. objTool.SetStarted(); it would print started on the console.
objTool inherits all the methods from the base class. You can use them as such or override them.

I know that.
My prob. is, I would like to call some methodes from Base
by using objObjet. Because that objet has objet that I created dynamically from Class Tools

I should post a nother class that I created its objet dynamically

class Electric:Base
{
        protected int iVoltage;
        protected int iWatt;
}

What would be wrong in coding Electric MyElectricTool = new Electric();and MyElectricTool.SetStarted();?

I think the OP wants to know how to do a reflective method call.

My objectife is to make an object Electric that creat it in Class Tools. As you can see the name of dynamic objet is from string sTypeName (see in Class Tools). The name of object could be another name like : "Hydraulic or Mechanic" depends on what the user give and then I woud like to use it in Class Main to call some methodes in Class Base.

Someone told me to use this code in Class Main.:
Object result = obj.GetType().GetMethod("MaMethdode").Invoke(obj, new Object[] { "paramètre 1", "paramètre 2" });. But I don't know how to use it and What Should I put in "paramètre 1", "paramètre 2"

I hope you are understand what I mean. Thanks.

PS: Sorry, my english isn't parfect.

Did you notice that we all three live in Belgium? Hi Mathias, how are you?
Just to say your English is as good as mine.

This works but I don't know if it is this you want:

static void Main(string[] args)
        {
            /*
                 * Electric
                 * Hydraulic
                 * Mechanic
                 */
            Object objObject;
            Tools objTool = new Tools();
            objObject = objTool.CreateInstance("Electric");
            Electric MyElectricTool = (Electric)objObject;
            try
            {
                //objObject. ??? (how to call some methodes from Class Base ?)
                MyElectricTool.setStoppedUrgently(); //call a Base class method
            }
            catch (Exception)
            {
            }
            Console.Read();
        }

What Should I put in "paramètre 1", "paramètre 2"

It's in the library documentation:

An argument list for the invoked method or constructor.
This is an array of objects with the same number, order, and type as the parameters of the method or constructor to be invoked.
If there are no parameters, parameters should be null.

(Source: MethodBase.Invoke)

Here's an example that demonstrates a reflective method call to a method with no parameters:

using System;
using System.Reflection;

public class Test
{
    public static void Main(string[] args)
    {
        Type typeOfB = typeof(B);
        object obj = Activator.CreateInstance(typeOfB);
        MethodInfo methodToCall = typeOfB.GetMethod("myMethod");
        // null is passed as second argument because "myMethod" does not have parameters
        methodToCall.Invoke(obj, null);
    }
}

public class A
{
    public void myMethod()
    {
        Console.WriteLine("myMethod called");
    }
}

public class B : A
{}

// output:
// myMethod called

The example is mostly analog to your problem so you should be able to adapt it to your problem quite easily.

commented: Great effort, as always! +14

Thanks for the example and it works ;)

PS: Heureux de vous connaitre les gars.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.