Alternative C # attribute alternatives

Currently, I have created a class with ~ 30 properties to set. This is done to create the URL request later (i.e. http://www.domain.com/test.htm?var1=a&var2=b...&var30=dd ").

The problem I am facing is property names that do not necessarily match the names of the request variables (this should be different). For example, I might have a variable called "BillAddress", while the query variable should be "as_billaddress".

I have no control over the naming scheme of query variables, as they are installed in an external source.

One of the possible solutions that I used is to create a custom attribute and design the properties using its corresponding mappings:

[CustomQueryAttribute("as_billaddress")]
string BillAddress{get;set;}

To get the attribute, although it needs a little reflection, and because of more properties, I was curious if there is an easier way to perform this function. Not as much as setting / getting custom attributes without reflection, but the ability to bind a variable with a string variable to any property.

I also thought about how to configure each variable as a kind of KeyValuePair, each of them being a copy of the request, but I didn’t get into that idea too much.

To summarize / clarify my background above, what would you do to associate a string with a property (rather than a property value)?

As always, any comments are welcome.

+3
3

, , , - , ( ), / .

- :

static Dictionary<string, PropertyInfo> propertyMap = new Dictionary<string, PropertyInfo>();

static MyClass()
{
     Type myClass = typeof(MyClass);
     // For each property you want to support:
     propertyMap.Add("as_billaddress", MyClass.GetProperty("BillAddress"));
     // ...
}

... , .

+3

- , , ( , ).

+1

If you look at the popular ORM devices, then almost all either use custom attributes or some kind of XML mapping file. The advantage of the latter is that you can change the display without recompiling your application - the disadvantage is that it hurts in performance. However, I would say that your choice seems quite reasonable.

0
source

Source: https://habr.com/ru/post/1713382/


All Articles