c# - .NET 2.0: Invoking Methods Using Reflection And Generics Causes Exception -


I am new to stack overflow, so please forgive me. I have just started transitoning at C # and I got stuck on a problem.

I want to pass a generic class and want to call any method from that class. So, my code looks like this:

  Public Zero UpdateRecords & lt; T & gt; (T sender) where T: new () // where T: new () came from Resharper {type foo = type .GetType (. Sender.GetType () ToString ()); Object [] userParameters = new object [2]; UserParameters [0] = X; UserParameters [1] = y; Sender = new T (); // To see if I could solve my exception problem, MethodInfo populateRecord = foo.GetMethod ("MethodInOtherClass"); PopulateMethod.Invoke (Sender, userParameters); }  

Exception thrown: "Object references are not set to an instance of an object."

Again, I really am sorry, because I'm almost completely new to C # and this is the first time I've ever met reflection and generic thanks!

First of all, I run this code in a debugger and an "break exception" To help to isolate / em>, what is causing a line error. This is a useful debugging technique that can help you find these kinds of problems more quickly in the future. debug & gt; & Gt; Go to Check the checkbox in the Threshold column for Exception and Common Language Runtime Exceptions in VS.

Now for your problem it seems that sender has been passed as a null . If so, then line:

  type foo = Type.GetType (. Sender.GetType () ToString ()); Instead, you can use it:  
  type foo = typeof (T);  

A NullReferenceException

which identifies the type of generic parameter without the need for an example.

Now, without knowing more about your code, it is impossible to immediately say that it is the right thing to do an instance of T . Because ReSharper has recommended adding , where T: new () it is not appropriate - unless you know this is the correct behavior .

Finally, I do not know that there is a solid reason to use the reflection to call MethodInOtherClass - maybe there is. But since you are new to C #, I tell you that if the T type will always have a sub-class of some base type A or always some interface < Code> I is included in the method that you want to call, you can implement a general obligation so that the compiler knows this. Then you can call the method without using the reflection:

  Public Zero UpdateRecords & lt; T & gt; (T sender) where T: SomeBaseClass_Or_SomeInterface_ThatDefinesMethod {sender = new T (); SenderMandalineOper Class (X, Y); }  

Very good.

A last comment is unusual for passing a logic in some method, and then ignore it completely - just to instantify an example within the method. There are cases when it is appropriate - but I see it as one. If possible, I try to get rid of either the sender argument or to test the code for tap first and then immediately change it.


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -