Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emit metadata for auto-property initializers #10454

Open
dsaf opened this issue Apr 11, 2016 · 6 comments
Open

Emit metadata for auto-property initializers #10454

dsaf opened this issue Apr 11, 2016 · 6 comments

Comments

@dsaf
Copy link

dsaf commented Apr 11, 2016

I want to be able to know what are the default values of properties without having to refer to documentation similarly to how optional method arguments work.
Would it be possible to emit reflectable metadata for property initializers?
This would be beneficial for auto-generated documentation and IDE hints.

At the moment my choices are:

public string Name { get; set; } = "John";
  • + language idiomatic;
  • + guaranteed to execute;
  • + all values are supported;
  • - black box - not introspectable/reflectable without instantiation;
  • - documentation that is not generated from code is guaranteed to become out of sync;
  • - no IDE hints.
[DefaultValue("John")]
public string Name { get; set; }
  • - requires custom code;
  • - not guaranteed to execute;
  • - needs a version of attribute per type without a proper literal (e.g. DateTimeDefaultValue);
  • + reflectable;
  • + documentation is possible to generate from code;
  • + IDE hints are possible with custom code.

Related SO question:

http://stackoverflow.com/questions/33383991/c-sharp-6-0-and-reflection-getting-value-of-property-initializers

@dsaf
Copy link
Author

dsaf commented Apr 11, 2016

Of course optional method arguments have the following limitation:

A default value must be one of the following types of expressions:

  • a constant expression;
  • an expression of the form new ValType(), where ValType is a value type, such as an enum or a struct;
  • an expression of the form default(ValType), where ValType is a value type.

https://msdn.microsoft.com/en-us/library/dd264739.aspx

@DavidArno
Copy link

I want to be able to know what are the default values of properties without having to refer to documentation similarly to how optional method arguments work.

The obvious solution would be "look at the code". I'm guessing that isn't an option for you though?

@dsaf
Copy link
Author

dsaf commented Apr 11, 2016

@DavidArno It's a solution.

However it has a number of usability problems:

  1. requires mentally parsing unrelated code;
  2. requires active navigation as opposed to prompting a tooltip;
  3. requires code availability or decompilation;
  4. doesn't give a possibility of code hints, e.g. "You are setting this property to what is already it's default value within an object initializer, are you sure this is what you want to do?".

@svick
Copy link
Contributor

svick commented Apr 11, 2016

all values are supported

I think this would be a significant problem. How do you represent arbitrary expressions/values in metadata?

@dsaf
Copy link
Author

dsaf commented Apr 11, 2016

@svick Yes, I am afraid it might be a substantial problem.

@dsaf
Copy link
Author

dsaf commented Apr 11, 2016

Somewhat related to #711.

@jaredpar jaredpar added this to the Unknown milestone Apr 11, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants