.net - Can I apply attributes to object fields dynamically? -



.net - Can I apply attributes to object fields dynamically? -

i working on project, serialize/store objects in csv files. (yes aware not best practice, it's must.)

i'm trying maintain solution clean, have multiple projects within solution, 1 of them library project contains storage/serialization methods. project has reference csvhelper, 3rd party library i'ld utilize csv operations.

in project a

foo myobject = new foo(x,y,z); b.writetocsv(myobject);

in project b (my lib, referencing csvhelper)

public static writetocsv(foo myobject); // csvhelper work

however, csvhelper requires attributes set on foo object before can operate on it.

i trying avoid applying these attributes in foo object class definition file, instead prefer apply these attributes dynamically within project b.

my question is, possible apply attributes later on, dynamically? whole point maintain project clean possible, sense complicate things more. bad idea, or there mutual solution issue?

update on request, example:

my foo object definition:

public class foo { public string a; public string b; public int c; }

csvhelper requires attributes on fields this:

public class foo { [csvcolumn(name = "a", fieldindex = 1)] public string a; [csvcolumn(name = "b", fieldindex = 2)] public string b; [csvcolumn(name = "c", fieldindex = 3)] public int c; }

in order maintain code clean, want avoid adding these attributes here, because want maintain code related storing/serializing in separate library. (project b)

yes , no. in case - no.

you can hypothetically add together attributes dynamically requires quite different approach reflection. see http://msdn.microsoft.com/en-us/library/ms171819.aspxi uncertainty 3rd party library supports this.

i'd prefer associate types/objects kind of configuration when flexibility needed. if need attach values object consider conditionalweaktable.

but these variants unfortunately cannot used in situation.

if need separate concerns create dtos correctly applied attributes , utilize kind of mapper (e.g. automapper) convert entities.

.net attributes projects-and-solutions solution code-cleanup

Comments

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -