|
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
|
|
|
|
|
Hi,
you are passing "null" as the first parameter to method.Invoke. But the first parameter is the required instance that the method will be executed on. The instance must be an object of the type you loaded dynamically. So before calling invoke you have to create an object from the type "myClassLibrary.myClass". This can be done by reflection also, using the constructor.
Have a look here:
http://msdn.microsoft.com/en-us/library/system.type.getconstructor.aspx[^]
Regards
Sebastian
|
|
|
|
|
Hi Sebastian,
Thanks for the answer, but I still do not understand how to fix.
I am not a pro programmer. Please explain with example...
thanks
|
|
|
|
|
Hi,
rewrite the invocation like this:
method = type.GetMethod("Method1", p);
object myObject = Activator.CreateInstance(type);
Object[] ps = new Object[3] { val1, val2, 0 };
method.Invoke(myObject, ps);
This will create an instance named myObject and invokes the method "method1" on that instance.
Regards
Sebastian
|
|
|
|
|
Thanks Sebastian for your quick response. Anyhow I managed to solve this problem by changing a single line at the button2_Click method, as shown below.
Previous code:
p[0] = Type.GetType("System.Enum");
Now changed to:
p[0] = assembly.GetType("myClassLibrary.myClass+myEnum");
Thanks
Chris
|
|
|
|
|
You try to invoke a non-static method on a null object. Create an instance of the class the Type represents by using Activator.CreateInstance, and pass that instead of null.
|
|
|
|
|
I already have a code for my linked list. How do I connect notepad to c# to make it as a simple database of my program?
|
|
|
|
|
Hi,
do you mean notepad? The windows notepad? This is just an application to edit files. Do you want to store your data within a file? Then think about using a format like csv. How is the linked list connected to this? What do you want to store exactly?
Regards
Sebastian
|
|
|
|
|
Yessir. The windows notepad. I'll convert it .txt to xml so i can use it to store some data. So when I close my program, if i hit the load button, the data stored in the notepad would come up in the gridbox.
|
|
|
|
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System;
using System.Xml;
namespace Machine_Problem
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private LinkedList<StudentInfo> Stud = new LinkedList<StudentInfo>();
private void Form1_Load(object sender, EventArgs e)
{
}
private void AddEntry_Click(object sender, EventArgs e)
{
StudentInfo Temp = new StudentInfo();
Temp.ID = int.Parse(textBox2.Text);
Temp.LastName = textBox1.Text;
Temp.FirstName = textBox4.Text;
Temp.EMail = textBox3.Text;
Temp.CInfo = long.Parse(textBox5.Text);
Stud.AddLast(Temp);
MessageBox.Show(" Data entry succesful! ");
}
private void Clear_Click(object sender, EventArgs e)
{
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
textBox4.Clear();
textBox5.Clear();
}
private void button7_Click(object sender, EventArgs e)
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(" C:\Users\dbrylle\documents\db.xml ");
for (int i = 0; i < Stud.Count; i++)
{
StudentInfo temp;
temp = Stud.ElementAt<StudentInfo>(i);
string data;
data = temp.ID.ToString() + " -- ";
data += temp.LastName + ", ";
data += temp.FirstName + " -- ";
data += temp.CInfo + " - ";
data += temp.EMail;
}
}
that's my code for now. i'm going to use gridbox for this program but I can't seem to make notepad save the data that my program will accept.
Regards,
Dexter
|
|
|
|
|
Hello Everyone!!
I am really in some serious problem, can you ppl please help me!
Is there any PKCS11 wrapper in C# or .Net so that it can be used from C# applciation.
What I mean is that, there are typical function in PKCS11 (cryptoki.dll) like C_Login, C_Encrypt, or C_GetSlotInfo,
I am trying to write myself a Wrapper for PKCS11 but it seems the task would be eronomous, and I am already facing many problems.... I could not understand how to write cryptoki definied variables, structures and classes in C#.
Please any help would be appreciated..
Thanks in Advanced
Regards
|
|
|
|
|
|
hi everyone!
i have the uri object with following url:
http://localhost/res/index.php?time_id=1&a=search&search=yes&course_id=1
i want to edit this object, ex edit the time_id variable from '1' to '2'
and do this with other vaiables in this url
would u tell me how to do this and share example codes ?
thanks in advance.
|
|
|
|
|
That is only a string.
I use Replace for it :
string s = "..." // <- this is your uri address
s = s.Replace("time_id=1", "time_id=2");
and so on...
|
|
|
|
|
Thank you!
but the "time_id" available just is a example.In my situation,i just only edit the value of each available and neednt know what is available for a URL.
|
|
|
|
|
|
I,m looking for a code in Visual Studio to active keyboard's key instead of pressing any button in windows form. For example, when I want to Exit from windows form instead of pushing Exit button, I enter the Escape key. Hopefuly that was enough clear!!!
|
|
|
|
|
Set the form's "Cancel Button" property to the button that closes the form.
Then, when you press Escape, it will activate the code behind that button.
|
|
|
|
|
|
How can i make online calls to mobile phones? (That is, how can i write a program to make phone calls through the internt to my mobile phone)
|
|
|
|
|
Exactly what part of developing such an application are you having trouble with?
|
|
|
|
|
I have written a program(Server and Client). The client can communicate to the Server using remoting to chat and using VOIP to Voice chat online. I need the server to transfer my phone calls i make from the client to any mobile phones in any part of the world. How can i go about this? What are the procedures or the codes required to achieve this aim?
|
|
|
|
|
Hi guys, you are my last hope.
read carefully to the end of my problem: i'd like to drag&drop attachments from outlook to explorer; depending on the pressed key (eg. shift) the email should be edited with the file name (like "Attached file shifted to C://test/file.pdf") or the full path and filename where the file have been pasted (->left mouse, no key: standard copy&paste; left mouse + shift: cut&paste - editing email with filename; left mouse + ctrl: cut & paste - editing email with full path)
there are several programms which remove attachments from email and saving them in defined folders; but this is not the solution; i want to remove the files with my mouse to individual folders (eg. proper project folders...) and have the attachments removed from the email (you know outlook folder size)
most of the features are standard for programming or a little bit of google. the crunchpoint what i couldn't find after days of googling:
i need an event FIRED IN OUTLOOK (eg. within an add-on) when an attachment is pasted to explorer from an email including file name and paste folder;
idea 1)
WM_DROPFIlE and similar: this message is only sent to the application where the file is pasted; does not work
idea 2)
register as listener in explorer: i didn't find a proper interface in explorer
idea 3)
register as listener in outlook: i didn't find a proper interface in outlook; an additional problem would be gathering the information from explorer of the filename and paste-folder
guys, does any one of you has an idea?
why in the C# forum? i'd like to implement in C#
THANKS FOR YOUR HELP
werner
|
|
|
|
|
I was considering making a usercontrol for a WinForms app that was simply a combo style control that displayed a list of hyperlinks when it was dropped down.
Ideally I'd like to have an option to data bind it, but I'm not sure how to do it? Is it simply a matter of coding a DataSource and DataMember property so that when they're set, it loads the control? Or is their more to it?
Thanks
Everything makes sense in someone's mind
|
|
|
|
|
You might want to read the article on MSDN[^]
--edit--
The previous link points to the 'simple databinding walkthrough', but you'd need the complex one[^].
I are Troll
modified on Wednesday, October 21, 2009 5:32 PM
|
|
|
|