I'm trying to get a method using Type.GetMethod. There are two methods with
that same name, one is a standard method, the other is a Generic method.
How do I get the Generic method? Is there a BindingFlag that will only get
the Generic one? Here is an example ...
[TestClass()]
public class UserTest
{
public ArrayList GetCollection()
{ return new ArrayList(); }
public List<T> GetCollection<T >()
{ return new List<T>(); }
[TestMethod()]
public void GetCollectionTe st()
{
Type genType = typeof(UserTest );
MethodInfo info = genType.GetMeth od("GetCollecti on");
MethodInfo genInfo = info.MakeGeneri cMethod(typeof( User));
object obj = genInfo.Invoke( this, null);
List<User> users = obj as List<User>;
}
}
If I run this code, I get an AmbiguousMatchE xception. If I remove the first
GetCollection, it works. Any ideas?
thanks
~ Paul 6 17179
I haven't read through the c#2.0 spes, but i notice that GetCollection
and GetCollect<T> have the same signature, only differ by return type,
is it a confliction?
Paul Welter wrote: I'm trying to get a method using Type.GetMethod. There are two methods with that same name, one is a standard method, the other is a Generic method. How do I get the Generic method? Is there a BindingFlag that will only get the Generic one? Here is an example ...
I don't think there's a GetMethod overload that will distinguish your
GetCollection() from your GetCollection<T >() - so far as I can see,
you'll have to call GetMethods(), then filter by Name and
ContainsGeneric Parameters.
-- www.midnightbeach.com
Paul Welter wrote: I'm trying to get a method using Type.GetMethod. There are two methods with that same name, one is a standard method, the other is a Generic method. How do I get the Generic method? Is there a BindingFlag that will only get the Generic one? Here is an example ...
[TestClass()] public class UserTest { public ArrayList GetCollection() { return new ArrayList(); }
public List<T> GetCollection<T >() { return new List<T>(); }
[TestMethod()] public void GetCollectionTe st() { Type genType = typeof(UserTest ); MethodInfo info = genType.GetMeth od("GetCollecti on"); MethodInfo genInfo = info.MakeGeneri cMethod(typeof( User)); object obj = genInfo.Invoke( this, null); List<User> users = obj as List<User>; } }
If I run this code, I get an AmbiguousMatchE xception. If I remove the first GetCollection, it works. Any ideas?
You might be able to GetMethod("GetC ollection<>"), because it's really
the non-constructed Generic type information you are interested in. I
guess that should suffice for Reflection to make the distinction between
the two methods.
Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
> You might be able to GetMethod("GetC ollection<>"), because it's really the non-constructed Generic type information you are interested in. I guess that should suffice for Reflection to make the distinction between the two methods.
GetMethod("GetC ollection<>") does not work. The documentation has a note
that says...
"The name parameter cannot include type arguments. For example, the C# code
GetMethod("MyGe nericMethod<int >") searches for a method with the text name
"MyGenericMetho d<int>", rather than for a method named MyGenericMethod that
has one generic argument of type int."
Of cource it doesn't say how to search for a generic.
thanks
~ Paul
Paul Welter wrote: GetMethod("GetC ollection<>") does not work. The documentation has a note that says...
"The name parameter cannot include type arguments. For example, the C# code GetMethod("MyGe nericMethod<int >") searches for a method with the text name "MyGenericMetho d<int>", rather than for a method named MyGenericMethod that has one generic argument of type int."
Of cource it doesn't say how to search for a generic.
You are right. I confused this with the way you can look at Generic
types for classes, e.g. "Type type = typeof(Collecti on<>)".
Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
You might be able to use GetMethod("GetC ollection`1").
But to figure it out, why not write some code that prints out the names of
all the methods in your class (with Type.GetMethods ())?
"Oliver Sturm" <ol****@sturmne t.org> wrote in message
news:OI******** ******@TK2MSFTN GP14.phx.gbl... Paul Welter wrote:
I'm trying to get a method using Type.GetMethod. There are two methods with that same name, one is a standard method, the other is a Generic method. How do I get the Generic method? Is there a BindingFlag that will only get the Generic one? Here is an example ...
[TestClass()] public class UserTest { public ArrayList GetCollection() { return new ArrayList(); }
public List<T> GetCollection<T >() { return new List<T>(); }
[TestMethod()] public void GetCollectionTe st() { Type genType = typeof(UserTest ); MethodInfo info = genType.GetMeth od("GetCollecti on"); MethodInfo genInfo = info.MakeGeneri cMethod(typeof( User)); object obj = genInfo.Invoke( this, null); List<User> users = obj as List<User>; } }
If I run this code, I get an AmbiguousMatchE xception. If I remove the first GetCollection, it works. Any ideas?
You might be able to GetMethod("GetC ollection<>"), because it's really the non-constructed Generic type information you are interested in. I guess that should suffice for Reflection to make the distinction between the two methods. Oliver Sturm -- omnibus ex nihilo ducendis sufficit unum Spaces inserted to prevent google email destruction: MSN oliver @ sturmnet.org Jabber sturm @ amessage.de ICQ 27142619 http://www.sturmnet.org/blog This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: AxlsPixel |
last post by:
Hi All,
I have a class (called CTestClass) within which I have a method
(Foo). This method has the following signature:
Foo(int x, int y, ref int z)
I am attempting to use reflection to invoke this method but before
doing that I want to use Type.GetMethod to bind to it and validate
it's signature. The problem is this binding fails due to the ref
|
by: phancey |
last post by:
I am trying to invoke a web service method dynamically. I have created
a generic function that takes a method name, string of parameters and
calls the web method using System.Reflection:
MethodInfo mi = proxyInstance.GetType().GetMethod(methodName);
object paramsArray = (object)methodParams.ToArray(typeof(object));
object result = mi.Invoke(proxyInstance, paramsArray);
where methodParams is an array of parameters where the value...
|
by: Marek |
last post by:
Hi
I need to call various functions in a native C++ DLL (FORTRAN eventually
too) - passing integers, doubles, (pointers and arrays to both of these as
well) and ultimately structures too. I was quite happily proceeding using
ModuleBuilder.DefinePInvokeMethod until I started implementing calls to
pointer (byref) parameters. The switch to TypeBuilder.DefinePInvokeMethod
(the one with the required custom modifiers) has resulted in the...
|
by: Ympostor |
last post by:
Why can't I do this (it gives a syntactic error):?
foreach (System.Reflection.PropertyInfo oProp in
oObject.GetType().GetProperties())
{
if ((oProp.PropertyType.IsGenericType) &&
(oProp.PropertyType.GetGenericTypeDefinition() ==
(typeof(IList<>))))
{
Type oMyType = oProp.PropertyType.GetGenericArguments();
|
by: Marc Gravell |
last post by:
Given a generic method "of T", is there a good way of ensuring that any
static ctor on T has executed? Following code demonstrates (TestClass1)
that via generics you can use the Type instance long before the static
ctor fires.
With a generic class and the "new()" clause, I can force this in the
static ctor of the generic class, but this is not always convenient,
plus may (possibly?) be seen as a no-op .
Sample; ideally I'd get "Static...
| |
by: D2 |
last post by:
Hi,
I have a requirement where I need to call a generic method without
knowing the class name i.e. I'm getting class name from xml file.
Given below is a replica of the scenario we are having. The generic
method shown is just to explain the scenario, actually its somewhere
outside.
private void Form1_Load(object sender, EventArgs e)
{
|
by: =?ISO-8859-1?Q?Norbert_P=FCrringer?= |
last post by:
Hello,
Imagine, that I've got a string variable containing a value of each
possible value type (string, int32, double, ...). Now I want to
convert the string to an object of the right type, where the correct
type is given by a Type variable.
So given is:
string strValue;
Type typeValue;
|
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.
|
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();
|
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,...
|
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...
| |
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,...
|
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...
|
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...
|
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();...
|
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...
|
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
| |
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...
| |