c# - How to warn if a property is accessed without being initialized, by adding an attribute? -



c# - How to warn if a property is accessed without being initialized, by adding an attribute? -

i'd technique automatically replace next code:

[warnifgetbutuninitialized] public int myproperty {get; set; }

with this:

/// <summary> /// property warns if value fetched before has been instantiated. /// </summary> private bool backingfieldispopulated = false; private int backingfield; public int myproperty { { if (backingfieldispopulated == false) { console.writeline("error: cannot fetch property before has been initialized properly.\n"); homecoming 0; } homecoming backingfield; } set { backingfield = value; backingfieldispopulated = true; } }

i'd prefer solution works @ compile time, reflection slow.

i aware aspect oriented programming (aop) (e.g. postsharp , ccisharp) i'd interested if there other method accomplish this.

update

see how utilize postsharp warn if property accessed before has been initialized? has link sample code demonstrates technique using postsharp.

attributes add together meta info classes , class members - nothing.

the operations done on decorated members done through reflection - there back upwards in of existing tooling, there no such back upwards custom attributes.

in short - can't have attribute checked @ compile time if compiler not have back upwards it.

you can, however, create own code snippets create such code easier write , create.

c# .net visual-studio

Comments

Popular posts from this blog

delphi - blogger via idHTTP : error 400 bad request -

c++ - compiler errors when initializing EXPECT_CALL with function which has program_options::variables_map as parameter -

How do I check if an insert was successful with MySQLdb in Python? -