C#中静态成员继承的解决方法 [英] Workaround for static member inheritance in C#

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

问题描述

出于我的目的,我有许多基于数据库的对象的基类(不完全是,但是为了清楚起见,让它保持这种方式).
所有派生类都需要一个TableName成员,该成员包含数据库表的名称.

我需要的是一种满足以下所有条件的方法:

For my purposes I have a base class for a number of database-based objects (not exactly but let's keep it this way for clarity).
All of the derived classes need a TableName member which contains the name of the database table.

What I need is a way to fulfill all of the following points:

  1. 以下函数应适用于所有派生类:

  1. The following function should work with all of the derived classes:


List<T> ReadAll<T>() where T: BaseClass, new()
{
    List resultList = new List();
    var results = doQuery("SELECT * FROM " + T.TableName);
    foreach (var r in results)
    {
        resultList.Add(new T(r));
    }
    return resultList;
}
 

(从表中读取所有记录-需要表名)

(reads all records from the table - needs the table name)

从基类继承必须迫使我在创建新的派生类时添加/重写TableName成员,否则代码不应编译(无论如何,这是其目的之一);

Inheritance from the base class needs to compel me to add/override the TableName member whenever I create a new derived class, otherwise the code should not compile (it's one of its purposes anyway);

TableName成员应该是静态的(或达到该目的的某种东西),因为我不需要为第1点而访问常量成员的派生类型的假"实例.

The TableName member should be static (or something to that purpose), since I shouldn't need a "fake" instance of the derived type just to access a constant member for the sake of point 1.

现在,假设我的目的是明智的,并且不是基于错误的假设,那么考虑到C#(出于充分的理由)不允许继承静态成员,我如何才能实现所有这些目的?
相反,如果您认为我认为所有观点都可以同时实现是错误的,那么您认为我应该如何更改设计呢?

Now, assuming my purposes are sensible and not based on wrong assumptions, how can I attain all of these purposes considering that (for good reasons) C# doesn't allow inheritance of static members?
If instead you think I'm wrong in assuming that all of the points should be attainable together, how do you think I should change my design?

推荐答案

您不能覆盖静态成员,因此为了每个类不同而拥有一个静态字段通常是行不通的.如果每个类都需要这样的数据,那么您在谈论的是元数据,它可以使用属性来创建

You can't override static members, so having a static field for the sake of being different per class usually won't work. If you need such data per class, you're talking about metadata, which can be created using Attributes

[AttributeUsage(AttributeTargets.Class)]
public class TableNameAttribute : Attribute
{
    private readonly string _name;
    public string Name {get { return _name; } }

    public TableNameAttribute(string name) { _name = name; }
}

然后在每个班级

[TableName("Derived")]
public class DerviedClass { /*...*/ }

这在编译时不能被接受,因此,当给定的类中不存在[TableName]时,您需要确保添加适当的处理.如果需要强制确保始终存在该属性,请编写一些好的单元测试,也许不行,它会扫描您的程序集并检查是否正确标记了所有类型,或者遵循所有代码必须在提交之前运行一次"规则.请注意,通过在运行时进行良好实践和测试来强制执行此操作,您还可以更轻松地避免因表名称拼写错误而不可避免的麻烦.

This cannot be cheked at compile time, however, so you'll need to make sure to add proper handling when [TableName] is not present on a given class. If you need to be compelled to always make sure the attribute is present, write some good unit tests, perhaps noe which scans your assembly and checks for all types being tagged appropriately, or follow the "all code must be run once before comitting" rule. Note that by enforcing this at runtime with good practices and testing, you'll also more readily dodge the inevitable headache of a mispelled table name.

但是,使用属性,除非您编写用于访问表名称属性的静态属性,否则您将不需要将泛型限制为单数类型.

Using attributes, though, you won't need to constrain your generic to a singular type, unless you write a static property to access the table name attribute.

这篇关于C#中静态成员继承的解决方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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