Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# custom attribute shorthad

Tags:

c#

.net

Lets say I have an attribute with a fairly long/complex list of parameters. For example: [DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV","|DataDirectory|\\stores.csv","stores#csv", DataAccessMethod.Sequential)]

Is there a way to create a custom attribute as a shorthand for this, so that instead of typing the above for every method, I can just use [CustomDataSource]?

like image 344
azar g. Avatar asked Mar 21 '26 00:03

azar g.


1 Answers

The DataSource attribute is sealed, you cannot inherit from it.

The easiest solution is to use constants:

public class Const
{
    public const string Provider = "Microsoft.VisualStudio.TestTools.DataSource.CSV";
    public const string ConnString = "|DataDirectory|\\stores.csv";
    public const string Table = "stores#csv";
    public const string Method = DataAccessMethod.Sequential;
}

[DataSource(Const.Provider, Const.ConnString , Const.Table , Const.Method)]
like image 115
Dávid Molnár Avatar answered Mar 22 '26 12:03

Dávid Molnár



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!