Reference inheriting class in base class method? - c#

I'm looking for the best way to provide a method in an (abstract) base class, which all inheriting classes should be able to use.
This method needs to reference fields and properties of the inheriting types.
Is there a way to provide such a prototype method which doesn't require me to either:
Pass a reference to each inheriting instance in question
Implement a method on each inheriting class which passes a reference to itself to the base class's method
Write an extension method for implementing classes
All of the above work, but seem somewhat inconvenient and unelegant in their own way.
Here is an example where I implemented the three above methods of referencing the inheriting class:
using System;
namespace Test
{
public abstract class BaseClass
{
public void ReferenceInheriting(object InheritingInstance)
{
Console.WriteLine("Do things specific to the inheriting class or instance thereof: " + InheritingInstance.GetType().Name);
}
}
public class Inheriting : BaseClass
{
public void MakeUseOfBaseClassImplementation()
{
base.ReferenceInheriting(this);
}
}
public static class Extensions
{
public static void BeAvailableForAllImplementing(this BaseClass Inh)
{
Console.WriteLine("Do things specific to the inheriting class or instance thereof: " + Inh.GetType().Name);
}
}
class program
{
public static void Main(string[] args)
{
Inheriting inh = new Inheriting();
Console.WriteLine("Method 1: Calling the inherited method from an inheriting instance, passing a reference to the instance:");
inh.ReferenceInheriting(inh);
Console.WriteLine("Method 2: Implementing call to the base class's method in own class:");
inh.MakeUseOfBaseClassImplementation();
Console.WriteLine("Method 3: Extension method for all implementing classes:");
inh.BeAvailableForAllImplementing();
}
}
}
The three approaches all produce the same output, but have drawbacks.
Short of parsing caller information, is there another way to do this?
It's not a big deal of course, but I'm interested in making this method as user friendly as possible, both for implementing inheritance and for calling.
Thank you!

You don't need any of that.
This:
public void reflectInheriting(object inheritingInstance)
{
Console.WriteLine("Do things specific to the inheriting class or instance thereof: " + inheritingInstance.GetType().Name);
FieldInfo fi = inheritingInstance.GetType().GetField("getMe");
Console.WriteLine(fi.GetValue(inheritingInstance));
}
Can be rewritten as simply:
public void reflectInheriting()
{
Console.WriteLine("Do things specific to the inheriting class or instance thereof: " + this.GetType().Name);
FieldInfo fi = this.GetType().GetField("getMe");
Console.WriteLine(fi.GetValue(this));
}
And that's all you need.
C# saves the actual underlying type of an object regardless of how it's cast, so even inside BaseClass, this.GetType() will be Inheriting.
Program output to prove nothing has changed:
Method 1: Calling the inherited method from an inheriting instance, passing a reference to the instance:
Do things specific to the inheriting class or instance thereof: Inheriting
Use me in BaseClass
Method 2: Implementing call to the base class's method in own class:
Do things specific to the inheriting class or instance thereof:
Inheriting
Use me in BaseClass
Method 3: Extension method for all implementing classes:
Do things specific to the inheriting class or instance thereof: Inheriting
Use me in BaseClass

You could use Reflection, but that is kind of a weapon of last resort. The idiomatic way to access a property or method in a descendent class is to make it abstract in the base class.
public abstract class BaseClass
{
public void GetInheriting()
{
Console.WriteLine("GetMe is: {0}", this.GetMe);
}
protected abstract string GetMe { get; }
}
public class Inheriting : BaseClass
{
protected override string GetMe => "Use me in BaseClass";
public void MakeUseOfBaseClassImplementation()
{
base.GetInheriting();
}
}
public class Program
{
static public void Main()
{
var o = new Inheriting();
o.MakeUseOfBaseClassImplementation();
}
}
Link to Fiddle
Output:
GetMe is: Use me in BaseClass

Related

How to use Type of inheriting class as parameter

I have a base class (can also be an interface) and n-classes that extend the base class.
I want to have a function that excepts only classes of the base class type.
Currently I am doing something like this
abstract class BaseClass{
public abstract void Execute();
}
class MyClass : BaseClass {
public void Execute(){
//my code
}
}
[...]
MyFunction(Type param)
{
//check if param is type of BaseClass. If not, throw exception
}
The problem with this implementation is, that I can pass any type of class. My goal is to prevent this.
Like in TypeScript you can do this
myFunction(param: {new (): BaseClass}){
//my code
}
Is there a similar approach I can use in C#?
There's a couple of things you can do, depending on what you actually want to do in the function.
Stating your actual use case makes it easier to come up with a solution.
Looking at the typescript code it looks like you want to be able to call the constructor inside the function to produce an instance of the given type that derives from BaseClass.
In this case I'd try using generics:
public void MyFunction<T>() where T: BaseClass, new()
{
T myClass = new T();
//Do stuff...
}
This works with interfaces/classes. It will only accept types deriving from BaseClass.
Edit: updated my answer after being notified how my previous answer wouldn't know if it was a grandchild of the base class
public abstract class BaseClass
{
public abstract void Test();
}
public class DerivedFromBase : BaseClass
{
public override void Test(){}
}
public class Grandchild : DerivedFromBase
{
}
public class Main
{
Grandchild aClass= new Grandchild(); // Instantiate Normally
UseIfOnlyTypeOfBaseClass(aClass);// Use Method with check like this
}
private void UseIfOnlyTypeOfBaseClass<T>(T param)
{
if (typeof(T).IsSubclassOf(typeof(BaseClass)))
{
Console.WriteLine("Match");
}
}

How can I cause a derived method to be called during an inherited base constructor?

I'm trying to call derived methods in a base constructor. I'd hoped the code below would result in a print of "Hello Derived" (specifically due to calling ParseData as a method of this.
using System;
public class Program
{
public static void Main()
{
Derived a = new Derived("Hello");
}
public class Base
{
public Base(string s)
{
this.ParseData(s);
}
protected void ParseData(string s)
{
Console.WriteLine(s+ " Base");
}
}
public class Derived : Base
{
public Derived(string s) : base(s){}
new private void ParseData(string s)
{
Console.WriteLine(s + " Derived");
}
}
}
I understand from the answer to this previous question why it doesn't work as I'd hoped it would - but can anyone provide suggestions of how best to implement my desired effect without resorting to having to call ParseData in my calling code after construction is complete every time this object in constructed - as below - if at all possible. (I'm likely to have a lot of different derived classes to do this on...)
Thanks!
using System;
public class Program
{
public static void Main()
{
string s = "Hello";
Derived a = new Derived(s);
a.ParseData(s);
}
public class Base
{
public Base(string s)
{}
protected void ParseData(string s)
{
Console.WriteLine(s+ " Base");
}
}
public class Derived : Base
{
public Derived(string s) : base(s){}
new private void ParseData(string s)
{
Console.WriteLine(s + " Derived");
}
}
}
As you have realised, you cannot safely call a virtual method from a base class' constructor.
C# allows you to do so, but the call to the derived class's overridden method will be executed in a context where the derived class's fields have not been initialised - which is likely to spell disaster.
There is an approach you can take to mitigate this problem. It's not perfect, since it puts the onus on derived classes to do things properly, but does at least mean that client code that is creating the objects doesn't need to do anything special.
It goes like this:
Make the base class AND the method you want to call abstract in the base class declaration. This is to force derived classes to implement it. (You can give the method a body in the base class if there's some common code you want to call from derived classes.)
Make the constructor of the base class protected. This is not strictly necessary if the base class is abstract, but it is conventional to do so.
Each derived class must implement the abstract method from the base class.
Make the constructor of every derived class private so that you can force client code to use a factory method to create it (see later step).
Each derived class must implement a static factory method that can be used to create an instance of the class. This method should call the overridden method.
It sounds like a lot, but actually it boils down to the following for your example:
public abstract class Base
{
protected Base(string s)
{
// Do something with s? Remove if not needed!
}
// Make this abstract so it must be overridden by a derived class.
protected abstract void ParseData(string s);
}
public class Derived : Base
{
public static Derived Create(string s)
{
var result = new Derived(s);
result.ParseData(s);
return result;
}
private Derived(string s) : base(s) // Private to force use of Create().
{
// Whatever.
}
protected override void ParseData(string s)
{
Console.WriteLine(s + " Derived");
}
}
Now when client code wants to create an instance of the derived class, it must use Derived.Create(s) to do so rather than new Derived(s) (which won't compile due to the constructor being private).
And when the client code does create an instance of the derived class, the derived ParseData() will always be called, e.g.:
static void Main()
{
var a = Derived.Create("Hello");
}
Note: I've made ParseData() protected, like you did in your original code, but it doesn't have to be protected; it could be public if necessary.
In your base class, you need to add the abstract or virtual modifier to the method you want to override in specific implementations.
abstract:
You only declare the method signature in the base class and all derived class MUST implement it. Note: the class must also be abstract.
virtual:
Similar to abstract, but with this, you can provide a method body that will act as a default implementation for derived classes. Basically, you'll have the choice to override it or not in your derived classes.
Here's an example using virtual
using System;
public class Program
{
public static void Main()
{
string s = "Hello";
Derived a = new Derived(s);
a.ParseData(s);
}
public class Base
{
public Base(string s)
{}
protected virtual void ParseData(string s)
{
Console.WriteLine(s+ " Base");
}
}
public class Derived : Base
{
public Derived(string s) : base(s){}
protected override void ParseData(string s)
{
Console.WriteLine(s + " Derived");
}
}
}
You can't do this safely in C#. Constructors are executed in order from the least specific to most specific - in your case, Base followed by Derived. To call a method on Derived from Base, you would need to make a virtual (or abstract) method on Base. But calling that in the Base constructor is a bad idea - the Derived constructor didn't execute yet! The override method might rely on stuff that isn't initialized.
If you can, instead of using a constructor, expose a public static method for each type. This allows you to safely initialize the object, call your virtual method, and return the finished type.
By using new you completely hide a member from your base-class. So actually you have two members that tend to have the exact same name. So when you have a reference of type Derived, the new member is used, otherwiese the base-class member is used.
In your case you have a new private member. This way you not only hide the base-class member, you also make it impossible to anyone outside Derived to access the new member, because it is private.
Best would be to make your base-class member virtual and the override it in your derived class:
public class Base
{
public Base(string s)
{}
protected virtual void ParseData(string s)
{
Console.WriteLine(s+ " Base");
}
}
public class Derived : Base
{
public Derived(string s) : base(s){}
protected override void ParseData(string s)
{
Console.WriteLine(s + " Derived");
}
}
If you can´t change your base-class, you have to use new. However you have to use an access-modifier which allows access from the outside, e.g. internal:
public class Derived : Base
{
public Derived(string s) : base(s){}
internal new void ParseData(string s)
{
Console.WriteLine(s + " Derived");
}
}
This assumes Derived is in the same assembly as your Main.
Apart from this you may get issues when calling virtual members in your constructor. It may work, if you do not use any members that are not yet initialized because of the order in which the constructor are executed - from most basic to most derived. However I´d stronly discourage you from doing so. See this question for details: Virtual member call in a constructor

Defining factory methods in interfaces or abstract superclasses of the product class

I have an abstract super class and subclasses inheriting from it.
Each subclass MySubclass shall have a public static MySubclass CreateFrom(ISomething something) factory method. The interface of its argument is the same for all subclasses, but the return type must of course always be the type of the respective subclass.
Can I somehow achieve this to have static factory methods following an interface or abstract superclass method definition without creating a separate static factory class for each single subclass?
If the ISomething is always of the same (or at least a common) type, you could make the CreateFrom method of the superclass generic and Invoke the constructor of the inherited class with the parameter. Just make sure all your inherited classes have that constructor (Not sure but I don't think there is a way to 'force' a constructor pattern).
public abstract class SuperClass
{
public static T CreateFrom(ISomething something)
{
return (T)Activator.CreateInstance(typeof(T), something);
}
}
public class InheritedClass : SuperClass
{
public InheritedClass(ISomething something)
{}
}
This way you can create instances by calling
SuperClass.CreateFrom<InheritedClass>(something);
Or you split the creation and initialization:
public abstract class SuperClass
{
protected abstract void Initialize(ISomething something);
public static T CreateFrom(ISomething something) where T : new()
{
T result = new T();
T.Initialize(something);
}
}
public class InheritedClass : SuperClass
{
public InheritedClass()
{}
protected override Initialize(ISomething something)
{}
}
You can´t define static members on interfaces as static members belong to a certain class. However I can´t imagine of a reason to use this. You should ask yourself why you need such a functionality. Does a sub-class really have to instantiate itself or can the same easily be done with another independent (factory-)class?
Just create one simple factory-class with a generic parameter that indicates what to create.
class Factory<T> where T: new()
{
T CreateFrom(ISomething param)
{
return new T();
}
}
Now you can simply call it like this:
var myFactory = new Factory<MyClass>();
myFactory.CreateFrom(mySomething);
I resorted to a different solution in similiar kind of requirement. In my superclass which happened to be an abstract one I required to create an instance of subclass to do something with it so I did the following trick:
public abstract class Element
{
protected virtual void copyFrom(Element other)
{
}
protected abstract Elememt newInstanceOfMyType();
public object clone()
{
var instance= newInstanceOfMyType();
instance.copyFrom(this);
return instance;
}
}
Now all my subclasses inheriting from Element class required to override newInstanceOfMyType method to give away instance of its type and then also override copyFrom method to produce a perfect clone. Now people might argue that why an abstract Clone method cant do the same job? Yes it can. But I required cloned instance of subclass as well as an empty instance(without copying anything from current) so I came up with this architecture.

Get attribute of derived C# class passed as base class to generic method

I'm trying to determine the value of an attribute on a derived class, when it's been passed into a method through a base class parameter.
For example, complete code sample below:
class Program
{
static void Main(string[] args)
{
DerivedClass DC = new DerivedClass();
ProcessMessage(DC);
}
private static void ProcessMessage(BaseClass baseClass)
{
Console.WriteLine(GetTargetSystemFromAttribute(baseClass));
Console.ReadLine();
}
private static string GetTargetSystemFromAttribute<T>(T msg)
{
TargetSystemAttribute TSAttribute = (TargetSystemAttribute)Attribute.GetCustomAttribute(typeof(T), typeof(TargetSystemAttribute));
if (TSAttribute == null)
throw new Exception(string.Format("Message type {0} has no TargetSystem attribute and/or the TargetSystemType property was not set.", typeof(T).ToString()));
return TSAttribute.TargetSystemType;
}
}
public class BaseClass
{}
[TargetSystem(TargetSystemType="OPSYS")]
public class DerivedClass : BaseClass
{}
[AttributeUsage(AttributeTargets.Class)]
public sealed class TargetSystemAttribute : Attribute
{
public string TargetSystemType { get; set; }
}
So, in the above example, I had intended that the generic GetTargetSystemFromAttribute method returns "OPSYS".
But, because the DerivedClass instance has been passed to ProcessMessage as the base class, Attribute.GetAttribute is not finding anything because it's treating the DerivedClass as the Base Class, which does not have the attribute or the value I'm interested in.
In the real-world there are dozens of Derived Classes, so I was hoping to avoid lots of:
if (baseClass is DerivedClass)
...which is suggested as the answer in the question How to access the properties of an instance of a derived class which is passed as a parameter in the form of the base class (which relates to a similar issue, but with properties). I was hoping because I'm interested in Attributes there's a nicer way of doing it, especially as I have dozens of derived classes.
So, here's the question. Is there any way I can obtain the TargetSystemType value of the TargetSystem Attribute on my derived classes in a low-maintenance way?
You should change this line:
(TargetSystemAttribute)Attribute.GetCustomAttribute(typeof(T), typeof(TargetSystemAttribute));
with this:
msg.GetType().GetCustomAttributes(typeof(TargetSystemAttribute), true)[0] as TargetSystemAttribute;
P.S. GetCustomAttributes returns array and I picked up first element for example where only 1 attribute is expected, you may need to change, but the logic is the same.

Getting the default constructor of a generic abstract class

I have a generic abstract base class from which I would like to derive from a dynamic type built through reflection.emit. Also I need to customize the derived class default constructor to initialize some fields. To build the default constructor of the derived class correctly I need to obtain the default constructor of the base class and invoke it. The problem is that I'm not being able to get the default constructor from the base class.
An example:
public abstract class Test<T>
{
private T data;
public abstract void Go();
}
public class TestDerive : Test<int>
{
public override void Go()
{
}
}
class Program
{
static void Main(string[] args)
{
ConstructorInfo[] constructors = typeof(Test<>).GetConstructors();
int length = constructors.Length;
}
}
I've tried everything and length is always zero. I don't understand. I've inspected similar cases in the reflector and there's indeed a call to the base constructor of the abstract class. The question is how do I get it to do the same ?
The default constructor of an abstract class is protected - you need to specify binding flags to access it via reflection. You can use
typeof(Test<>).GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance)
(It makes no practical difference whether it's public or protected in normal code, of course, as you can't call the constructor other than from a derived type.)
Note that this is the case whether or not the class is generic - it's the abstractness that's causing you problems.

Categories