Topaz Filer: if you use e-mail for business, we can save you money and decrease your risk.
Type.InvokeMember on indexed properties and array fields
Messages   Related Types
This message was discovered on microsoft.public.dotnet.languages.csharp.


Guven Demir
Hi,

The help for Type.InvokeMethod() has the following code sample for reading an indexed property:

===

//Get an indexed property value
int index = 3;
result = t.InvokeMember ("Item",
BindingFlags.Default |
BindingFlags.GetProperty,
null, c, new object [] {index});
Console.WriteLine ("Item[{0}] == {1}", index, result);

===

I have a similar situation in my own class but when I use very similar code I get a MissingMethodException. My member is an instance of a sub class of my class which has an indexer. Something like:

===

class Foo
{
public class Bar
{
private int[] prInts=new int[] {1, 2, 3};

public int this[int index] { return prInts[index]; }
}

public Bar Integers=new Bar();
}

===

Then I use a code like

===

Foo obj = new Foo();
Type t = typeof(Foo);
int index = 2;
object result = t.InvokeMember ("Item",
BindingFlags.Default |
BindingFlags.GetProperty,
null, obj, new object [] {index});

===

which results in a MissingMethodException.

The same problem applies for array fields.

Any ideas? Thanks in advance.
Reply to this message...
Vote that this is a GOOD answer...
 
Auto-following on Twitter
Ubuntu and XP on one “desktop”
 
    
Nicholas Paldino [MVP]
Guven,

Use the Instance binding flag. It says in the documentation that either
the Instance or Static member of the BindingFlags enumeration needs to be
specified.

Hope this helps.

--
- Nicholas Paldino [MVP]
- Click here to reveal e-mail address

"Guven Demir" <_abidik_@_mail_._com_> wrote in message
news:3BFAEFFC.7FD302A6@_mail_._com_...
[Original message clipped]

of my class which has an indexer. Something like:
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer...
 
Outlook interop - stopping user properties appearing on Outlook message print
Seriously, why is “cut and paste” majorly newsworthy???
 
 
System.Console
System.MissingMethodException
System.Reflection.BindingFlags
System.Type




Ad
BootFX
Reliable and powerful .NET application framework.
Recession Busting Bespoke Software
Get through the recession by investing in bespoke software to decrease costs and create commercial opportunities.
Other DN247 Network Sites
.NET 247
SQL Server Wins
Old Skool Developer
 
Copyright © AMX Software Ltd 2008-2009. Portions copyright © Matthew Baxter-Reynolds 2001-2009. All rights reserved.
Contact Us - Terms of Use - Privacy Policy - .NET 247 is a member of the DN247 Network - 4.0.30129.1734