Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using config settings in attributes

Tags:

c#

.net

I have a fragment of C# code that looks like this:

[OracleCustomTypeMapping(Constants.DBSchema + ".TAB_VARCHAR2_250")]
public class StringTableFactory : TableFactoryTemplate<StringTable>
{
    public override System.Array CreateStatusArray(int length)
    {
        return new OracleUdtStatus[length];
    }
}

Is there any way to change the attribute declaration, so Constants.DBSchema is read from web.config instead of having it hardcoded as a constant in code? If I put ConfigurationManager.appSettings in the attribute declaration, I am getting "An attribute argument must be a constant expression..." error.

Thanks.

like image 720
user1044169 Avatar asked Sep 04 '13 16:09

user1044169


1 Answers

Rather than passing it in as part of the constructor arguments, read it direct from the ConfigurationManager in the Attribute's constructor.

public class OracleCustomTypeMappingAttribute : Attribute
{
    public OracleCustomTypeMappingAttribute(string typeName)
    {
        var schema = ConfigurationManager.AppSettings["Schema"];
        TypeMapping = schema + "." + typeName;
        // Or whatever property needs to be set
    }
}

Then you would just do:

[OracleCustomTypeMapping("TAB_VARCHAR2_250")]
public class StringTableFactory : TableFactoryTemplate<StringTable>
{
    public override System.Array CreateStatusArray(int length)
    {
        return new OracleUdtStatus[length];
    }
}
like image 177
rossipedia Avatar answered Sep 20 '22 12:09

rossipedia