以下是我想要完成的一个例子:
abstract class DoSomething
{
static void DoWhateverItIsThatIDo()
{
Console.WriteLine("You asked the abstract class to work. Too bad.");
}
}
class InspireMe : DoSomething
{
static void DoWhateverItIsThatIDo()
{
Console.WriteLine("You are amazing.");
}
}
class InsultMe : DoSomething
{
static void DoWhateverItIsThatIDo()
{
Console.WriteLine("You aren't worth it.");
}
}
class Program
{
static void Main()
{
DoSomething worker = InsultMe;
worker.DoWhateverItIsThatIDo();
worker = InspireMe;
worker.DoWhateverItIsThatIDo();
}
}
我来自Python背景,其中方法本身可以是一个变量,然后可以调用它。看起来C#没有这个概念,但我试图完成类似的事情。
我的想法是我想要一个可以是抽象类型的变量,这样就可以在其中存在许多不同类型的子类型。所有这些亚型都有一定的方法。我希望能够将任何这些子类型分配给抽象类型的此变量,然后调用子类型中存在的静态方法。
在C#术语中,我希望能够将类分配给变量,而不是类的实例,然后调用该类' s静态方法。
工厂听起来可能在正确的道路上,但我不确定工厂本身如何能够生成对类的这些引用(而不是创建实例)
我可以修改它来使用实例,但是假设我想要生成每种类型的类的静态方法,所有这些类仍然继承自基类型。
我觉得最有可能做到这一点 - 有人可以提出建议吗?
答案 0 :(得分:3)
在您描述的意义上,您不能将类用作C#中的变量。反射本质上允许你将类型视为变量并动态调用它们上的静态成员,但它会很乱并且不是类型安全的。
通过使用单例模式,你基本上可以完成你想要做的事情:
interface IDoSomething
{
void DoWhateverItIsThatIDo();
}
class DoSomething : IDoSomething
{
private DoSomething() {}
internal static readonly IDoSomething Instance;
static DoSomething()
{
Instance = new DoSomething();
}
public void DoWhateverItIsThatIDo()
{
Console.WriteLine("You asked the abstract class to work. Too bad.");
}
}
class InspireMe : IDoSomething
{
private InspireMe() {}
internal static readonly IDoSomething Instance;
static InspireMe()
{
Instance = new InspireMe();
}
public void DoWhateverItIsThatIDo()
{
Console.WriteLine("You are amazing.");
}
}
class InsultMe : IDoSomething
{
private InsultMe() {}
internal static readonly IDoSomething Instance;
static InsultMe()
{
Instance = new InsultMe();
}
public void DoWhateverItIsThatIDo()
{
Console.WriteLine("You aren't worth it.");
}
}
class Program
{
static void Main()
{
IDoSomething worker = InsultMe.Instance;
worker.DoWhateverItIsThatIDo();
worker = InspireMe.Instance;
worker.DoWhateverItIsThatIDo();
}
}
答案 1 :(得分:1)
除了类和实例之外,您真正想要的是对具有特定签名的方法的引用,在您的情况下void ()
。
虽然无法为变量分配静态类,但可以以类型安全的方式为变量分配方法。在C#中,您通常会使用Action
或Func
的重载,具体取决于方法签名的外观。
为了使这更有趣,让我们假设您想要引用类似int Foo(string, bool)
的内容,只需使用类型为Func<string,bool,int>
的变量,并为其分配任何具有此签名的方法。
解决问题的代码大概如下:
class DoSomething
{
static void DoWhateverItIsThatIDo()
{
Console.WriteLine("You asked the abstract class to work. Too bad.");
}
}
class InspireMe
{
static void DoWhateverItIsThatIDo()
{
Console.WriteLine("You are amazing.");
}
}
class InsultMe
{
static void DoWhateverItIsThatIDo()
{
Console.WriteLine("You aren't worth it.");
}
}
class Program
{
static void Main()
{
Action worker = InsultMe.DoWhateverItIsThatIDo;
worker();
worker = InspireMe.DoWhateverItIsThatIDo;
worker();
}
}
答案 2 :(得分:1)
解决方法可能是在基本抽象类中声明类型为Action
的属性,该属性包含要调用的方法。然后在派生类实例化期间通过调用基类构造函数初始化此属性:
abstract class DoSomething
{
public Action DoWhateverItIsThatIDo { get; set; }
protected DoSomething() { DoWhateverItIsThatIDo = DoSomething.DoIt; }
protected DoSomething(Action whatAction)
{
DoWhateverItIsThatIDo = whatAction;
}
protected static void DoIt()
{
Console.WriteLine("You asked the abstract class to work. Too bad.");
}
}
class InspireMe : DoSomething
{
public InspireMe() : base(InspireMe.DoIt) { }
private static void DoIt()
{
Console.WriteLine("You are amazing.");
}
}
class InsultMe : DoSomething
{
public InsultMe() : base(InsultMe.DoIt) { }
private static void DoIt()
{
Console.WriteLine("You aren't worth it.");
}
}
class DoWhatBaseClassDoes : DoSomething
{
public DoWhatBaseClassDoes() : base() {}
}
class Program
{
static void Main(string[] args)
{
DoSomething worker = new InsultMe();
worker.DoWhateverItIsThatIDo();
worker = new InspireMe();
worker.DoWhateverItIsThatIDo();
// In this case base class method is invoked
worker = new DoWhatBaseClassDoes();
worker.DoWhateverItIsThatIDo();
}
}
答案 3 :(得分:0)
C#和Java都不能让你覆盖静态基类方法。
但是,您似乎仍在使用对象的引用(您的worker
变量),那么为什么不使用非静态类方法?
(如果这不是您打算做的,请澄清。)
答案 4 :(得分:0)
我不是100%确定这是你想要的,但我写了一个模拟Prototypal inheritance in C#的库。
public class Foo: ProtoObject {}
public class Bar: Foo {}
dynamic myFoo = new Foo();
dynamic yourFoo = new Foo();
dynamic myBar = new Bar();
myFoo.Prototype.Name = "Josh";
myFoo.Prototype.SayHello = new Action(s => Console.WriteLine("Hello, " + s));
yourFoo.SayHello(myBar.Name); // 'Hello, Josh'
这当然涉及大量使用dynamic
关键字,这可能没用,因为你会丢失大量的编译时检查。