473,626 Members | 3,291 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 2777
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
2889
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
2869
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
3033
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
9674
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
1627
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
4937
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
3607
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
2259
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
2352
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
8262
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8196
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8637
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
8364
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
8502
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7192
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
5571
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
4090
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1807
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.