Click here to Skip to main content
15,896,359 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
Im loading an Exe into my application using Assembly.LoadFile().
From that is it possible to get the Method of a particular class from that EXE.
And im having an instance of a Structure as a member variable of my application.After loading that exe, i have to fill that instance of the structure member variable of my application in that loaded assembly.The complexity is that loaded assembly will not have that structure datatype. how to accomplish this?
plz help me....
Thanks in advance
Posted
Updated 24-Nov-10 19:55pm
v2

yep, its possible...
you can get it over the example below.

here I have created a DummyClass

C#
namespace DummyClass
{
    public class DummyClass
    {
        public int Add(int x, int y)
        {
            return x + y;
        }

        public int Sub(int x, int y)
        {
            return x - y;
        }
    }
}


and I am going to Invoke pirticular method Add() of the DummyClass using reflection

private void CallReflection()
        {
            Assembly assembly;
            Type type;
            MethodInfo[] Methods;
            object result = null;
            
            object classObject;
            MethodInfo mdInfo;
            ParameterInfo[] ParameterTypes;
            string asmName = @"D:\Reflection\..\bin\Debug\DummyClass.dll";
            assembly = Assembly.LoadFile(asmName);
            type = assembly.GetType("DummyClass.DummyClass");
            Methods = type.GetMethods();
            classObject = Activator.CreateInstance(type);
            mdInfo = classObject.GetType().GetMethod("Add");
            
            ParameterTypes = mdInfo.GetParameters();
            if (ParameterTypes.Length > 0)
            {
                object[] argument ={ 9,6};
                result = mdInfo.Invoke(classObject, argument);
            }
            else
            {
                result = mdInfo.Invoke(classObject, null);
            }
            if (result != null)
                MessageBox.Show(result.ToString());
        }



Think it would help you to clarify your problem.

Cheers,
Vasanth
 
Share this answer
 
Comments
Ramya 2010 25-Nov-10 2:19am    
This is not what im expecting. let us consider my application is having
struct student
{
string name;
int age;
string grade;
float average;
}
dis structure type is not available in the loaded assembly.But i have to fill in that loaded assembly. how to do this?Is it possible?
Vasanth@n 25-Nov-10 3:49am    
use FieldInfo instead of MethodInfo

FieldInfo fdInfo;
fdInfo = type.GetField("name");
Ramya 2010 25-Nov-10 3:58am    
Thank You soo much......
Dalek Dave 25-Nov-10 4:43am    
Good Answer!
Struct and its fields sould be Public, otherwise we cant access it. only properties and methods only can access via MethodInfo, use FieldInfo instead.

make sure your struct and its fields were public

C#
public struct student
{
    public string name ;
    public int age;
    string grade;
    float average;

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    public int Age
    {
        get { return age; }
        set { age = value; }
    }

}
 
Share this answer
 
See here[^].
 
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