C#从静态方法调用抽象方法 [英] C# call abstract method from static method
问题描述
现在,我具有sutch结构:
公共抽象类ASqlWorker {
抽象受保护的DbParameter DbParameterConstructor(String paramName,Object paramValue);
}
现在,我可以打电话给抽象 > DbParameterConstructor 是从ASqlWorker的静态方法中获得吗?
逻辑上,我可以使abstractMethod成为静态对象(它从名称和实例创建DbParameter的继承者的实例值,并且不使用ASqlWorker的字段),但在C#中不允许使用。
btw,这是不允许的,因为使用未实现的静态方法可能会引起麻烦。但是,在我的情况下,不会,因为我的抽象方法受到保护。
我只想编写一些从DbParameter到新DbParameter [1]的隐式转换器。
我做到了!
大声笑,我在C#中实现了抽象静态方法!
我做了什么:
- 创建了抽象类 AbstractDbParameterConstructors ,它不包含任何状态,仅包含抽象方法。
-
ASqlWorker类现在可以从AbstractDbParameterConstructors继承而来。
公共抽象子类ASqlWorker< TPC>
其中TPC:AbstractDbParameterConstructors,new()
{...}
-
已声明的私有静态变量
私有静态TPC生成器= new TPC();
这里我都有类似抽象静态方法的东西,而且我受到保护,免受不良影响,标准的巫婆创建者试图保护我。
deatails: http://sourceforge.net/projects/sqlworker/
i am writing library, that can be used for with different database engines. Abstract class has abstract DbParameter constructor and uses classes from System.Data.Common.
Now i have sutch structure :
public abstract class ASqlWorker{
abstract protected DbParameter DbParameterConstructor(String paramName, Object paramValue);
}
now, can i call abstract DbParameterConstructor from a static method of ASqlWorker?
Logicaly, i could make abstractMethod static (it creates instance of an inheriter of DbParameter from name and value and doesn't use fields of ASqlWorker), but it is not allowed in C#. btw, it is not allowed because of usage of non-realised static method can cause troubles. But in my case it won't, because my abstract method is protected.
i just want to write some implicit convertor from DbParameter to new DbParameter[1] to have more flexible interfaces.
I did it!
lol, i implemented abstract static method in C# !
what did i:
- created abstract class AbstractDbParameterConstructors, that don't contains any state, only abstract methods.
class ASqlWorker now gets generic, inhereted from AbstractDbParameterConstructors.
public abstract partial class ASqlWorker<TPC> where TPC : AbstractDbParameterConstructors, new() { ... }
declared private static variable
private static TPC generator = new TPC();
here both i have something like abstract static method, and i am protected from undesirable effects, from witch creators of standard were trying to protect me.
deatails : http://sourceforge.net/projects/sqlworker/
这篇关于C#从静态方法调用抽象方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!