InvokeMember problems

May 4, 2011 at 12:27 PM
Edited May 4, 2011 at 12:46 PM

Hello people,

I am trying to run a Method (for SimpleCommand) with InvokeMember to have the ability to dynamically run Methods, which names I get from a string.

I do this:

Type calledType = Type.GetType("Name.with.Full.Namespace");
object o = Activator.CreateInstance((Type)calledType);
var s = calledType.InvokeMember("Execute_Command_1", BindingFlags.InvokeMethod, null, o, new[] { objParam });

the method I try to run is
 public void Execute_Command_1(object obj)
 {
      ....
My Problem is, that I can and probably don't want to create a new instance of my Class (it is a Cinch-ViewModel, as you might think).
The Activator is throwing an exception, because of the parameterless call of this class.
Is there an more elegant way to call my Method?
Thanks in advance.
Tobi
Edit:
Solution:
object o = Activator.CreateInstance((Type)calledType, new object[]{nullnullnullnullnull});
This is only for the returnValue. And this is unnecessary.
=> Problem solved on my own.
May 7, 2011 at 8:23 AM

I answered this over at codeproject where you asked the question too

May 7, 2011 at 10:43 AM

sorry for the double posting.

but I saw this as a different problem...

I apologise!

May 7, 2011 at 7:14 PM

Your query is basically a general reflection query, thing is you always need an instance (instantiated object which has method in it) to use Reflection. No getting away from that whatever you do you need an instance.

And as you say default constructor will be what you need, but by doing that, you will not be able to MEF in services via constructor, that is up to you to decide if that is an issue, I don't know what problem you are trying to solve, but I would always want my ViewModel created in correct state, which for me always means constructor arguments.

 

Anyway sounds like you have it sorted anyway right?

May 7, 2011 at 7:19 PM

One thing you could do is get all the services from the container directly to pass into the Activator.CreateInstance call, but if you know what parameters are required and how many of them there are (which you example with loads of nulls seems to imply), why not just new up the ViewModel instance.

 

I must be missing something, as I do not see why you are using Reflection here? Is it read from a database or something like that?

 

Anyway if you need to absolutely use activator create instance I would use some more reflection and work out what the constructor parameters are to create it correctly and try and obtain those parameters for the constructor either from the MEF container or from where ever these constructor objects should be fetched from. In terms of how to get them from the MEF container directly, you can do something like this

IUIVisualizerService uiVisualizerService  = 
                    ViewModelRepository.Instance.Resolver.Container.GetExport<IUIVisualizerService>().Value;
May 9, 2011 at 6:46 AM

thank you for the answers.

You are right, I need Reflection because I read from a XML to dynamically map Buttons to Commands.

But now I solved it a better way.

I have a VM where the View with the Buttons is bound to and where I have a lot of SimpleCommands.

Behind the Buttons is only one SimpleCommand, which is an Orchestrator to dynamically execute via Relfection. In this VM the Reflection is doing fine.

But from another VM, which also has to execute these SimpleCommands, these Commands are connected through the MEF Mechanism.

So, I had a knot in my brain, but no need to separately do the Reflection mechanism in this second VM. I only have to call the "Orchestrator"-Command in the first VM.

 

Thank you for your efforts and for your great framework!

May 9, 2011 at 6:57 AM

Glad you are now happy.

 

And as far as Cinch goes, yes I was very happy with it overall. I think it just works