Hi Nicholas,
Beside what you said I would like to add something:
You can't have polymorphism via reflection.
Consider this:
Case 1:
Type t = someObject.GetType();
In MSDN we can read the following for Object.GetType method:
"
....
Return Value
The Type instance that represents the exact runtime type of the current
instance.
.....
"
So polymorphism can't play any role here because we are going to call the
methods of the *actual* run-time type.
Well, we can get a type object for one of the parent classes and try to call
say
Type t = typeof(ABaseForSomeObjectsClass)
t.GetProperty("PropName").GetValue(someObject,.....);
And to say that we really need polymorphism here. It doesn't make sence,
though. Since we have the reference to the object we always can call:
someObject.GetType() to get the right type object. And we are back to square
one.
Case2:
Type t = typeof(SomeType);
If we need to access instance method we need to have reference to the actual
object. You already have my *Case1*...
If we we want to use a static member.... Well, we have already specified
the type. There is no room for polymorphism again. Mainly because we don't
have an object that has a *real* type.
And to back up my point here there is some example of this.
//I assume here that *static abstract* exist
class Base
{
public abstract static void Foo();
}
class Derived1: Base
{
public override static void Foo()
{
....
}
}
class Derived2: Base
{
public override static void Foo()
{
....
}
}
Somewhere in the code
Type t = typeof(Base)
So now, if we try to call Foo via reflection, which one should be called
Derived1.Foo or Derived2.Foo?
In this case polymorphism is not possible again.
Any other way to obtain a type object belongs to one of those two cases I
believe.
The real problem is that Type objects *represent* tha given type. They are
not references to actual meta-type objects (which in fact exist internally)
B\rgds
100
in message news:
[email protected]...
Chris,
In a previous thread, these reasonings were given as well. My believe
is that unless you can achieve polymorphism, then you shouldn't
introduce
a
new language construct like this which will need additional support through
another mechanism. Abstract already has a very well-defined meaning, and
that meaning would be diluted though this use of it.
If you have to resort to reflection anyways to do this, then you can
easily get around this by creating a custom attribute and applying it
to
the
static method that you want to represent your "overridden" method in another
class. Also, I think that using the design patterns pointed out
represent
a
very clean solution to the issue as well. Having to use a language
construct in conjunction with reflection is rather dirty. If I had to use
reflection to call overridden methods on derived classes, I don't
think
that
I would like it too much.
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
The problem with this comes from the base type (abstract) not
knowing
which derived type to go to for the implementation.
How would you indicate that you want to use the implementation in
Derived? I can have many implementations of DoSomething, and if I want
to
call through the Base class, there is no way of indicating which derived
class to use.
Fair enough. But I see having an abstract static member as more of a way
to
guarantee that any non-abstract subclass will have implemented that
member,
not as a way to accomplish polymorphism at all. For instance:
public abstract class ImporterBase {
protected abstract string[] ExtensionsSupported { get; }
public abstract void ImportFile(string path);
}
public class CsvImporter : ImporterBase {
protected override string[] ExtensionsSupported {
get {
return new string[] {"csv"};
}
}
public override void ImportFile(string path) {
//some stuff
}
}
Now, the only way this could really benefit a programmer, it seems, is
when
reflection is used to look at the derived class. For instance:
public abstract class ImporterBase {
//stuff from above definition
public static ImporterBase GetImporter(string extension) {
Type[] types = Assembly.GetExecutingAssembly().GetTypes();