Read attribute value of derived class

I'm building the app which will work with SQLite and what I want to do now is to get table creation strings for each entity which should be stored with code like this:

table_creation_string1 = Book.GetTableCreationString();
table_creation_string2 = Scroll.GetTableCreationString();

Which will allow me to do the minimal work with new entities creation using custom attributes to store table name and fields names.

Here is my setup example:

[System.AttributeUsage(System.AttributeTargets.Class)]
public class DBItemAttribute : System.Attribute
{
    public string TableName;
    public DBItemAttribute(string table_name)
    {
        TableName = table_name;
    }
}

internal abstract class DBItem
{
    ...
}


[DBItemAttribute("book_table")]
internal class Book : DBItem
{
    ...
}

[DBItemAttribute("scroll_table")]
internal class Scroll : DBItem
{
    ...
}

The problem I faced is that I can't get an attribute value for Book and Scroll classes without constructing the object. In other terms - using smth like that:

string table1 = Scroll.GetTableName();
string table2 = Book.GetTableName();

with next values output

"scroll_table"
"book_table"

So I'm looking for the best solutions for that or some good alternatives.

I've already learned that static method in base class will not work for that due it seems to be not possible to get derived class info calling static method defined in base.

UPD: The code like

table_creation_string1 = new Book().GetTableCreationString();

works for a sure, but I wonder if it can be done in way I've described above.

Upvotes: 1

Views: 139

Answers (3)

vasily.sib
vasily.sib

Reputation: 4002

Attributes is a properties of a class, so you don't need to create an instance of this class to get those values. You can create a separate helper-class for this:

internal static class DBHelpers
{
    public static string GetTableName<TDBItem>()
        where TDBItem : DBItem
    {
        var attribute = typeof(TDBItem).GetCustomAttribute<DBItemAttribute>();
        return attribute?.TableName;
    }
}

So now you can use it like this:

var tableName1 = DBHelpers.GetTableName<DBHelpers>();
var tableName2 = DBHelpers.GetTableName<Book>();

Upvotes: 2

Abhay Agarwal
Abhay Agarwal

Reputation: 89

try this, Add a class name and its correspondent table name, when you create new.

    public class ClassFactory
        {
            public static Action GetTableName(string className)
            {
                switch (className)
                {
                    case "Scroll": return "Book_table";
                    case "Book": return "Book_table";
                }
            }
         }

call this class :

string table1 = ClassFactory.GetTableName("Scroll");
string table2 = ClassFactory.GetTableName("Book");

Upvotes: 0

Jonathan Gagne
Jonathan Gagne

Reputation: 4389

Static class method

Just change internal class Book and internal class Scroll to static, as static class Book and static class Book. This is the whole purpose who the static classes.

Static Class Definition

A static class is a class that can't be instantiated. The sole purpose of the class is to provide blueprints of its inherited classes. A static class is created using the "static" keyword in C#. A static class can contain static members only. You can‘t create an object for the static class.

Instantiate and call the method Otherwise you can instantiate it and call the method like this: new Book().GetTableName()

Upvotes: 1

Related Questions