RE: Using InvokeMethod to circumvent compile-time type checking



Abstract classes can serve you well, but accepting an instance of an abstract
class merely to get around a contract is not a wise move (if this is your
motivation). This is the biggest danger I can see.

Another potential issue here is having to call methods that are outside of
the abstract class (derived class specific methods). In these cases, you end
up booting polymorphism and end up with a tightly coupled implementation that
has the false appearance of being loosely coupled. Rather than deleting
dependencies, you are obfuscating them

Those are two potential gotchas I can see.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************


"Jeff Stewart" wrote:

I've got several classes that are all concrete implementations of an
abstract class. I have a separate comparer class full of AreEqual()
methods, each with a signature that accepts a pair of concrete classes:

AreEqual(ConcreteA x, ConcreteA y)
AreEqual(ConcreteB x, ConcreteB y)
etc.

I've got another method designed to compare arrays of concrete types.
I'd like to have a single method,

AreEqual(Abstract[] x, Abstract[] y)

that calls the appropriate AreEqual() function above for all
permutations of the elements in the arrays.

Of course, the compiler wouldn't let me do this at compile-time, 'cause
even though the arrays are full of concrete instances, it can't perform
the (widening?) conversion from Abstract to ConcreteA or ConcreteB or
whatever the type may be.

So I started investigating reflection, and came up with this solution:
I created an additional AreEqual() method,

public bool AreEqual(Abstract x, Abstract y) {
Type t = this.GetType();
return (bool)t.InvokeMember("AreEqual", BindingFlags.InvokeMethod,
null, this, new Object[] {x, y});
}

And it works fine! I use that method in my array comparison and it
seems to work like a charm. But I'm worried that this was too easy,
and that I'm missing something. (The BindingFlags don't all seem to be
very clear.) Should it really be so trivial to bypass compile-time
type checking? Am I "cheating" doing this?

--
Jeff S.


.



Relevant Pages

  • Using InvokeMethod to circumvent compile-time type checking
    ... I have a separate comparer class full of AreEqual ... I've got another method designed to compare arrays of concrete types. ... Of course, the compiler wouldn't let me do this at compile-time, 'cause ...
    (microsoft.public.dotnet.framework)
  • Re: global constant variables in mex
    ... I am trying to pass a couple of values into a mex function. ... done at compile-time, either with a const value that the compiler ... Declare your arrays as pointers, ...
    (comp.soft-sys.matlab)
  • Re: Teaching new tricks to an old dog (C++ -->Ada)
    ... For fixed size arrays and containers it is true, ... by using compile-time checked assertions. ... a map might not be ...
    (comp.lang.ada)
  • Re: Teaching new tricks to an old dog (C++ -->Ada)
    ... For fixed size arrays and containers it is true, ... by using compile-time checked assertions. ... a map might not be ...
    (comp.lang.cpp)
  • Re: Schnittmenge n Arrays
    ... Micha wrote: ... > vergleichen und mir die Schnittmenge ausgeben zulassen. ... der jeweils zwei Elemente der Arrays vergleicht. ... Diesen Comparer nutzt du in einer abgeleiteten Array-Klasse zum Vergleich ...
    (microsoft.public.de.german.entwickler.dotnet.vb)

Loading