Click here to Skip to main content
15,900,973 members
Home / Discussions / C#
   

C#

 
QuestionClient Server Application C# Pin
Anjum12321-Oct-09 22:03
Anjum12321-Oct-09 22:03 
AnswerRe: Client Server Application C# Pin
SeMartens21-Oct-09 22:22
SeMartens21-Oct-09 22:22 
QuestionIEnumerable<t></t> Pin
mehran.asg21-Oct-09 20:18
mehran.asg21-Oct-09 20:18 
AnswerRe: IEnumerable Pin
SeMartens21-Oct-09 20:32
SeMartens21-Oct-09 20:32 
GeneralRe: IEnumerable Pin
mehran.asg21-Oct-09 20:39
mehran.asg21-Oct-09 20:39 
AnswerRe: IEnumerable Pin
Abhishek Sur21-Oct-09 21:17
professionalAbhishek Sur21-Oct-09 21:17 
AnswerRe: IEnumerable Pin
Lutosław21-Oct-09 21:59
Lutosław21-Oct-09 21:59 
QuestionType.GetType() Error Pin
Christopher Perumal21-Oct-09 19:54
Christopher Perumal21-Oct-09 19:54 
I am getting an error message like this 'Object reference not set to an instance of an object' when I try to invoke an assembly method dynamically. Please refer to the attached code snippets.
I need forum's help to solve this problem.

The main code snippet.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using myClassLibrary;

namespace DynamicallyLoadAssembly
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Assembly assembly;
MethodInfo method;
Type type;

string sPath = @"F:\VS2005 Projects\myClassLibrary\myClassLibrary\bin\Debug\myClassLibrary.dll";
int val1 = 7;
int val2 = 5;

try
{
assembly = Assembly.LoadFile(sPath);
type = assembly.GetType("myClassLibrary.myClass");

Type[] p = new Type[3];
p[0] = Type.GetType("System.Int32");
p[1] = Type.GetType("System.Int32");
p[2] = Type.GetType("System.Int32&");

method = type.GetMethod("Method1", p);
Object[] ps = new Object[3] { val1, val2, 0 };
method.Invoke(null, ps);

MessageBox.Show("ps = " + ps[2].ToString());

}
catch (Exception exp)
{
MessageBox.Show(exp.Message);

}
}

private void button2_Click(object sender, EventArgs e)
{
Assembly assembly;
MethodInfo method;
Type type;

string sPath = @"F:\VS2005 Projects\myClassLibrary\myClassLibrary\bin\Debug\myClassLibrary.dll";
int val2 = 5;

try
{
assembly = Assembly.LoadFile(sPath);
type = assembly.GetType("myClassLibrary.myClass");

Type[] p = new Type[3];
p[0] = Type.GetType("System.Enum");
p[1] = Type.GetType("System.Int32");
p[2] = Type.GetType("System.Int32&");

method = type.GetMethod("Method1", p);
Object[] ps = new Object[3] { myClass.myEnum.One, val2, 0 };

// The error 'Object reference not set to an instance of an object' occurs when invoking this method.
method.Invoke(null, ps);

MessageBox.Show("ps = " + ps[2].ToString());

}
catch (Exception exp)
{
MessageBox.Show(exp.Message);

}

}

}
}

The library code snippet.

using System;
using System.Collections.Generic;
using System.Text;

namespace myClassLibrary
{
public class myClass
{
public enum myEnum : int
{
One,
Two,
Three
};

public static void Method1(int a, int b, out int c)
{
c = a + b;
}

public static void Method2(myEnum a, int b, out int c)
{
c = 0;
switch (a)
{
case myEnum.One:
c = 1 + b;
break;
case myEnum.Two:
c = 2 + b;
break;
case myEnum.Three:
c = 3 + b;
break;
}

}
}
}

Thanks

Chris

AnswerRe: Type.GetType() Error Pin
SeMartens21-Oct-09 20:35
SeMartens21-Oct-09 20:35 
QuestionRe: Type.GetType() Error Pin
Christopher Perumal21-Oct-09 21:57
Christopher Perumal21-Oct-09 21:57 
AnswerRe: Type.GetType() Error Pin
SeMartens21-Oct-09 22:19
SeMartens21-Oct-09 22:19 
GeneralRe: Type.GetType() Error [Solved] Pin
Christopher Perumal21-Oct-09 23:07
Christopher Perumal21-Oct-09 23:07 
AnswerRe: Type.GetType() Error Pin
0x3c021-Oct-09 20:39
0x3c021-Oct-09 20:39 
QuestionLinked List with Notepad Database. Pin
Dexter Brylle Matos21-Oct-09 17:54
Dexter Brylle Matos21-Oct-09 17:54 
AnswerRe: Linked List with Notepad Database. Pin
SeMartens21-Oct-09 20:38
SeMartens21-Oct-09 20:38 
GeneralRe: Linked List with Notepad Database. Pin
Dexter Brylle Matos22-Oct-09 19:28
Dexter Brylle Matos22-Oct-09 19:28 
GeneralRe: Linked List with Notepad Database. Pin
Dexter Brylle Matos22-Oct-09 19:43
Dexter Brylle Matos22-Oct-09 19:43 
QuestionRquire PKCS11 Wrapper in C# - Please Help Pin
sreekanth8121-Oct-09 17:35
sreekanth8121-Oct-09 17:35 
AnswerRe: Rquire PKCS11 Wrapper in C# - Please Help Pin
stancrm21-Oct-09 17:40
stancrm21-Oct-09 17:40 
Questionhow to manager the uri in C# Pin
silva10321-Oct-09 16:36
silva10321-Oct-09 16:36 
AnswerRe: how to manager the uri in C# Pin
stancrm21-Oct-09 17:34
stancrm21-Oct-09 17:34 
GeneralRe: how to manager the uri in C# Pin
silva10321-Oct-09 17:46
silva10321-Oct-09 17:46 
AnswerRe: how to manager the uri in C# Pin
silva10322-Oct-09 4:03
silva10322-Oct-09 4:03 
QuestionUsing keyboard's key instead of the buttom. Pin
niammirzaei21-Oct-09 16:22
niammirzaei21-Oct-09 16:22 
AnswerRe: Using keyboard's key instead of the buttom. Pin
Richard Andrew x6421-Oct-09 16:47
professionalRichard Andrew x6421-Oct-09 16:47 

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

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