tags:

views:

178

answers:

3

Hi...i need to execute a method "FindAll" in my page, but can be with any class. This method return a list of the object.

This is my method that i execute "FindAll". FindAll requires an int and returns an List of these class.

public void ObjectSource(int inicio, object o)
        {
            Type tipo = o.GetType();
            object MyObj = Activator.CreateInstance(tipo);
            object[] args = new object[1];
            args[0] = inicio;
            List<object> list = new List<object>();
            object method = tipo.InvokeMember("FindAll", BindingFlags.Default | BindingFlags.InvokeMethod, null, null, args);
        }

When i execute ObjectSoiurce, it returns ok, but i can't access the result. In VS2008, i can visualize the list by "ctrl + Alt + q" but by casting doenst work.

I forgot to say: this method "FindAll" is static!

please, help.

+1  A: 

Few things going on here, first, your method doesn't return the result.

Second, when you do return the object, there's nothing stopping you casting to the appropriate type in the calling code.

Third, you could use Generics to make this method strongly typed like so:

public T ObjectSource<T>(int inicio, T o)
{
  Type tipo = typeof(T);
  object MyObj = Activator.CreateInstance(tipo);
  object[] args = new object[1];
  args[0] = inicio;
  return tipo.InvokeMember("FindAll", BindingFlags.Default | BindingFlags.InvokeMethod, null, null, args) as T; 
}
Wim Hollebrandse
Hi...1) This method doesn't have to return any result. That is correct.2) The FindAll method returns an IEnumarable and use yeld statement.
ChepA
@ChepA: If the `FindAll` method returns an `IEnumerable` (whether using `yield` or not, it doesn't matter), then how do you expect to get at that `IEnumerable` if it isn't returned from the `ObjectSource` method?
Daniel Pryden
Also, as Jon Skeet pointed out in his comment on the question, creating a new object instance is probably the wrong thing to be doing here. Don't you want to call `FindAll` on the `o` object?
Daniel Pryden
As you said, FindAll *RETURNS* an IEnumerable...which in your case you can't even get at as your method returns void. Peachy.
Wim Hollebrandse
I changed my method as Wim sad, using generics and the same structure. I dont understand why ObjectSource must return somethig because this code is the begining. I need the "FindAll" returns to populate my gridview. How can i get "FindAll" return?
ChepA
@ChepA: You are calling `ObjectSource`, which in turn calls `FindAll`. Since the call to `FindAll` is *inside* the `ObjectSource` method, the only way for the code that calls `ObjectSource` to get `FindAll`'s return value is for `ObjectSource` to pass the return value along. The easiest way is for `ObjectSource` to just return the return value from `FindAll`. See my answer for a code example that does just that.
Daniel Pryden
Back to basics on this one, it seems.
Wim Hollebrandse
+1  A: 

Try this (updated):

public IEnumerable ObjectSource(int inicio, object o) {
    Type type = o.GetType();
    object[] args = new object[] { inicio };
    object result = type.InvokeMember("FindAll", 
        BindingFlags.Default | BindingFlags.InvokeMethod, null, o, args);
    return (IEnumerable) result;
}

A better solution would be to put your FindAll method into an interface -- say, IFindable, and make all your classes implement that interface. Then you can just cast the object to IFindable and call FindAll directly -- no reflection required.

Daniel Pryden
+1 for the interface suggestion, I suspect that is a far better solution
Pharabus
Good idea, but always when I cast result, returns null.
ChepA
When i execute object result = o.InvokeMember("FindAll", BindingFlags.Default | BindingFlags.InvokeMethod, null, null, args);, the returm type is {FrameWork.Patrimonio.Bem.FindAll}.
ChepA
@ChepA: Perhaps you should post the source to your `FindAll` method then.
Daniel Pryden
@ChepA: Don't do `o.InvokeMember`, that was a typo on my part, sorry. Do `type.InvokeMember` instead. Does that work?
Daniel Pryden
Also, can you post an example of how you are calling the `ObjectSource` method? How are you passing the `o` argument?
Daniel Pryden
@Daniel: i got a few object that i need to bind a grid view and this list more than 1.000 records, then a want to pagging by 50, and this Object Source must be generic because will call FindAll of the classes!
ChepA
@ChepA: Post this comment as a separate question and I'll give you a code example of how to do this. (And somebody else will probably give you a better answer as well.)
Daniel Pryden
ok.....i did it. When i execute myType.GetMethods() it returns{System.Collections.Generic.IEnumerable`1[FrameWork.Patrimonio.Bem] FindAll(Int32)}, why the compiler put "`1"?
ChepA
@ChepA: Because `FindAll` isn't actually returning an `IEnumerable`, it's returning an `IEnumerable<FrameWork.Patriomonio.Bem>`. The compiler uses the ```1` notation to differentiate the generic `IEnumerable<>` from the non-generic `IEnumerable`.
Daniel Pryden
A: 

Daniel, i got a few object that i need to bind a grid view and this list more than 1.000 records, then a want to pagging by 50, and this Object Source must be generic because will call FindAll of the classes!

ChepA
When I said to post this as a separate **question**, I meant, click on the "Ask Question" link at the top of the page and ask a completely new question. What you've posted here is an **answer** to your own question that actually just raises a new question. Remember, StackOverflow isn't a general-purpose forum, it's a question and answer site.
Daniel Pryden
LoL... But i did my reflection work! I'm very appreciate the help.
ChepA