473,702 Members | 2,679 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Invoking Generic Methods with Generic Arguments

Hi,
I'm having an issue with invoking a Generic method that takes Generic Arguments.

My method signature is
public void GenericMethodWi thGenericArgume nts<E, V>(List<EtheFir stList, List<VtheSecond List);

I pass the name of the method, the arguments for the "GenericMethodW ithGenericArgum ents" to another method, which is supposed to invoke this method using the Invoke method in the MethodInfo class.

My process of invocation is as follows

public static void InvokeGenericMe thod(object theInstance, string theMethodToInvo ke, object[] theMethodArgume nts, Type[] theArgumentType s,
Type[] theArgumentPara meterTypes)
{

MethodInfo testMethodToInv oke = null;
MethodInfo[] instanceMethods = null;

// Get all the instance methods
instanceMethods = theInstance.Get Type().GetMetho ds();

// Go through each method in the instance
foreach (MethodInfo method in instanceMethods )
{

// Search for the method to inovke.
if (theMethodToInv oke.Equals(meth od.Name))
{
testMethodToInv oke = method.MakeGene ricMethod(theAr gumentTypes);
if (method.Contain sGenericParamet ers)
{
Type[] genericParamete rs = new Type[theArgumentType s.Length];
for (int i = 0; i < genericParamete rs.Length; ++i)
{
if (theMethodArgum ents[i].GetType().IsGe nericType)
{
Type genericDefiniti on = theArgumentType s[i].GetGenericType Definition();
genericParamete rs[i] = genericDefiniti on.MakeGenericT ype(theArgument ParameterTypes[i]);

}
else
{
genericParamete rs[i] = theMethodArgume nts[i].GetType();
}
}
testMethodToInv oke = method.MakeGene ricMethod(gener icParameters);
}

}
}

try
{
// Call method under test
testMethodToInv oke.Invoke(theI nstance, theMethodArgume nts);
}
catch (Exception e)
{
}

}

My method class is InvokeGenericMe thod(instance, "GenericMethodW ithGenericArgum ents", new object[] { list1, list2 }, new Type[] {typeof(List<in t>), typeof(List<dou ble>)}, new Type[]{typeof(int), typeof(double)} );

The exception I get says {"Object of type 'System.Collect ions.Generic.Li st`1[System.Int32]' cannot be converted to type 'System.Collect ions.Generic.Li st`1[System.Collecti ons.Generic.Lis t`1[System.Int32]]'."}.

The question I have is with the two lines below:

Type genericDefiniti on = theArgumentType s[i].GetGenericType Definition();
genericParamete rs[i] = genericDefiniti on.MakeGenericT ype(theArgument ParameterTypes[i]);
Why does my argument expected wrap the List into another List of int's instead of a List of int's? I'm not sure how to figure this out with the reading material I have. I went through the MSDN documentation and could not figure out why my arguments are failing.
Any help to fix this, or pointers to any documentation for this issue will be much appreciated.
Thanks
Suds

May 7 '07 #1
1 2782
On Mon, 07 May 2007 11:02:14 -0700, Suds <na*******@nosp am.nospamwrote:
Hi,
I'm having an issue with invoking a Generic method that takes Generic
Arguments.

My method signature is
public void GenericMethodWi thGenericArgume nts<E, V>(List<E>
theFirstList, List<VtheSecond List);
I'm having trouble parsing your code. You formatted it, which makes it
hard to read, and there seems to be a bunch of stuff in there not related
to your actual question, which makes it hard to focus on what actually is
important.

However, that said...just looking at your usage and your question, I am
guessing that the fundamental issue is a mistake in your generic method
declaration. That is, you have specified that the parameters will be
List<Eand List<Vtypes, and then the E and V types you pass in are
themselves lists. This results in the parameters being lists of lists. I
think what you actually want is this:

public void GenericMethodWi thGenericArgume nts<E, V>(E, V) where E :
List<Object>, V : List<Object>;

Or something like that (generics are still new to me, and I'm not certain
about the exact syntax for the contraints). The idea being that you
constrain the parameters to types that are themselves generic lists
containing Object instances.

Pete
May 7 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2893
by: Jim Newton | last post by:
hi all, i'm relatively new to python. I find it a pretty interesting language but also somewhat limiting compared to lisp. I notice that the language does provide a few lispy type nicities, but some very important ones seem to be missing. E.g., the multiple class inheritance is great, but there are no generic functions (at least that i can find). If i have classes X, Y, and Z, and subclasses X_sub, Y_sub, and Z_sub respectively.
49
2879
by: Steven Bethard | last post by:
I promised I'd put together a PEP for a 'generic object' data type for Python 2.5 that allows one to replace __getitem__ style access with dotted-attribute style access (without declaring another class). Any comments would be appreciated! Thanks! Steve ----------------------------------------------------------------------
18
3043
by: Steven Bethard | last post by:
In the "empty classes as c structs?" thread, we've been talking in some detail about my proposed "generic objects" PEP. Based on a number of suggestions, I'm thinking more and more that instead of a single collections type, I should be proposing a new "namespaces" module instead. Some of my reasons: (1) Namespace is feeling less and less like a collection to me. Even though it's still intended as a data-only structure, the use cases...
7
9675
by: Andy Fish | last post by:
Hi, I'm trying to figure out how to invoke a method on an object given the object and the method name. For example, here's a simple object with a method: function MyObj (pName, pDesc) { this.name = pName; this.desc = pDesc; MyObj.prototype.foo = function() {
6
1630
by: LordHog | last post by:
Hello all, My lead wants to implement a data range monitor for a project that we are coding. Basically it performs a boundry checking that will take three parameters. I am/was trying to implement a generic data range monitor, but it doesn't quite work. I am trying to create one method that will accept any type of parameter and use those values whether they be int, unsigned int, float, double etc.... I am not sure if this can be done...
4
4939
by: Harold Howe | last post by:
I am running into a situation where the compiler complains that it cannot infer the type parameters of a generic method when one of the function arguments is an anonymous method. Here is a complete code example: //----------------------------- using System; using System.Collections.Generic;
26
3613
by: raylopez99 | last post by:
Here is a good example that shows generic delegate types. Read this through and you'll have an excellent understanding of how to use these types. You might say that the combination of the generic delegate type expression in just the right place and a well-named method means we can almost read the code out loud and understand it without even thinking. Note that since 'boxing' and 'unboxing' is involved (I think), you don't get what you...
3
2260
by: Anders Borum | last post by:
Hi I need to invoke a generic method determined at runtime. The method has two arguments, a string and a generic type that is constrained to a struct: public void Add<T>(string key, T value) where T : struct The method is an instance member located on a class called CmsPropertyManager. I also have a number of other "Add" methods with different overloads.
0
2359
by: =?Utf-8?B?TW9ydGVuIFdlbm5ldmlrIFtDIyBNVlBd?= | last post by:
"Anders Borum" wrote: Hi Anders, I'm afraid the GetMethod() does not currently support filtering on generic parameters so you will have to loop through the existing methods using GetMethods() MethodInfo methods = t.GetMethods();
0
9234
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9089
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8983
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7831
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5907
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4667
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3107
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2402
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2036
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.