C#从静态方法调用抽象方法 [英] C# call abstract method from static method

查看:30
本文介绍了C#从静态方法调用抽象方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写库,可用于不同的数据库引擎.抽象类具有抽象 DbParameter 构造函数并使用 System.Data.Common 中的类.

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.

现在我有了 sutch 结构:

Now i have sutch structure :

public abstract class ASqlWorker{

 abstract protected DbParameter DbParameterConstructor(String paramName, Object paramValue);

}

现在,我可以从 ASqlWorker 的静态方法调用抽象 DbParameterConstructor 吗?

now, can i call abstract DbParameterConstructor from a static method of ASqlWorker?

逻辑上,我可以使 abstractMethod 成为静态(它从名称和值创建 DbParameter 的继承者的实例,并且不使用 ASqlWorker 的字段),但在 C# 中是不允许的.顺便说一句,这是不允许的,因为使用未实现的静态方法会导致麻烦.但在我的情况下不会,因为我的抽象方法是受保护的.

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.

我只是想写一些从 DbParameter 到 new DbParameter[1] 的隐式转换器以获得更灵活的接口.

i just want to write some implicit convertor from DbParameter to new DbParameter[1] to have more flexible interfaces.

推荐答案

我做到了!

lol,我在 C# 中实现了抽象静态方法!

lol, i implemented abstract static method in C# !

我做了什么:

  • 创建了抽象类AbstractDbParameterConstructors,不包含任何状态,只包含抽象方法.
  • 类 ASqlWorker 现在变得通用,从 AbstractDbParameterConstructors 继承.

  • 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.

    细节:http://sourceforge.net/projects/sqlworker/

    这篇关于C#从静态方法调用抽象方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆