在基础 class 方法中引用继承 class?
Reference inheriting class in base class method?
我正在寻找在(抽象)基础 class 中提供方法的最佳方法,所有继承 classes 都应该能够使用。
该方法需要引用继承类型的字段和属性。
有没有一种方法可以提供这样一个不需要我做的原型方法:
- 传递对每个有问题的继承实例的引用
- 在每个继承 class 上实现一个方法,将对自身的引用传递给基础 class 的方法
- 写一个扩展方法来实现classes
以上所有工作,但以他们自己的方式似乎有些不方便和不优雅。
这是一个例子,我实现了上述三种引用继承的方法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();
}
}
}
这三种方法都产生相同的输出,但都有缺点。
除了解析来电者信息,还有其他方法吗?
这当然没什么大不了的,但我有兴趣使这个方法尽可能对用户友好,无论是实现继承还是调用。
谢谢!
你不需要这些。
这个:
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));
}
可以简单地改写为:
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));
}
这就是您所需要的。
C# 保存对象的实际基础类型,不管它是如何转换的,所以即使在 BaseClass
内部,this.GetType()
也将是 Inheriting
.
程序输出证明没有任何变化:
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
您可以使用反射,但这是不得已的武器。访问后代 class 中的 属性 或方法的惯用方法是在基础 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();
}
}
输出:
GetMe is: Use me in BaseClass
我正在寻找在(抽象)基础 class 中提供方法的最佳方法,所有继承 classes 都应该能够使用。
该方法需要引用继承类型的字段和属性。
有没有一种方法可以提供这样一个不需要我做的原型方法:
- 传递对每个有问题的继承实例的引用
- 在每个继承 class 上实现一个方法,将对自身的引用传递给基础 class 的方法
- 写一个扩展方法来实现classes
以上所有工作,但以他们自己的方式似乎有些不方便和不优雅。
这是一个例子,我实现了上述三种引用继承的方法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();
}
}
}
这三种方法都产生相同的输出,但都有缺点。
除了解析来电者信息,还有其他方法吗?
这当然没什么大不了的,但我有兴趣使这个方法尽可能对用户友好,无论是实现继承还是调用。
谢谢!
你不需要这些。
这个:
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));
}
可以简单地改写为:
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));
}
这就是您所需要的。
C# 保存对象的实际基础类型,不管它是如何转换的,所以即使在 BaseClass
内部,this.GetType()
也将是 Inheriting
.
程序输出证明没有任何变化:
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:
InheritingUse 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
您可以使用反射,但这是不得已的武器。访问后代 class 中的 属性 或方法的惯用方法是在基础 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();
}
}
输出:
GetMe is: Use me in BaseClass