How can I use PostSharp to replace this:
[WarnIfGetButUninitialized] public int MyProperty {get; set; }
Wherein:
/// <summary> /// Property which warns you if its value is fetched before it has been specifically instantiated. /// </summary> private bool backingFieldIsPopulated = false; private int backingField; public int MyProperty { get { if (backingFieldIsPopulated == false) { Console.WriteLine("Error: cannot fetch property before it has been initialized properly.\n"); return 0; } return backingField; } set { backingField = value; backingFieldIsPopulated = true; } }
Refresh
I should also add that this is a good method to improve code reliability. In a project with 20,000 lines, it's nice to know that everything is correctly initialized before using it. I intend to use this to build the Debug and remove it in the Release build, because I don't want to slow down the final release unnecessarily.
source share