Note that there are some explanatory texts on larger screens.

plurals
  1. POResolving a member name at runtime
    primarykey
    data
    text
    <p>Given a <em>type</em>, a <em>name</em> and a <em>signature</em>, how can I do a member lookup of the member with name <em>name</em> and signature <em>signature</em> using the C# rules of 7.4 (the 7.4 is the chapter number from the C# Language Specification) (or at least part of them... Let's say I can live with an exact match, without conversions/casts) <strong>at runtime</strong>? I need to get a <code>MethodInfo</code>/<code>PropertyInfo</code>/... because then I have to use it with reflection (to be more exact I'm trying to build an <code>Expression.ForEach</code> builder (a factory able to create an Expression Tree that represent a <code>foreach</code> statement), and to be pixel-perfect with the C# <code>foreach</code> I have to be able to do duck-typing and search for a <code>GetEnumerator</code> method (in the collection), a <code>Current</code> property and a <code>MoveNext</code> method (in the enumerator), as written in 8.8.4)</p> <p>The problem (an example of the problem)</p> <pre><code>class C1 { public int Current { get; set; } public object MoveNext() { return null; } } class C2 : C1 { public new long Current { get; set; } public new bool MoveNext() { return true; } } class C3 : C2 { } var m = typeof(C3).GetMethods(); // I get both versions of MoveNext() var p = typeof(C3).GetProperties(); // I get both versions of Current </code></pre> <p>Clearly if I try <code>typeof(C3).GetProperty("Current")</code> I get an <code>AmbiguousMatchException</code> exception.</p> <p>A similar but different problem is present with interfaces:</p> <pre><code>interface I0 { int Current { get; set; } } interface I1 : I0 { new long Current { get; set; } } interface I2 : I1, I0 { new object Current { get; set; } } interface I3 : I2 { } </code></pre> <p>Here if I try to do a <code>typeof(I3).GetProperties()</code> I don't get the <code>Current</code> property (and this is something known, see for example <a href="https://stackoverflow.com/questions/358835/getproperties-to-return-all-properties-for-an-interface-inheritance-hierarchy">GetProperties() to return all properties for an interface inheritance hierarchy</a>), but I can't simply flatten the interfaces, because then I wouldn't know who is hiding who.</p> <p>I know that probably this problem is solved somewhere in the <code>Microsoft.CSharp.RuntimeBinder</code> namespace (declared in the <code>Microsoft.CSharp</code> assembly). This because I'm trying to use C# rules and member lookup is necessary when you have dynamic method invocation, but I haven't been able to find anything (and then what I would get would be an <code>Expression</code> or perhaps a direct invocation).</p> <p>After some thought it's clear that there is something similar in the <code>Microsoft.VisualBasic</code> assembly. VB.NET supports late binding. It's in <code>Microsoft.VisualBasic.CompilerServices.NewLateBinding</code>, but it doesn't expose the late bounded methods.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload