Created
September 8, 2017 13:49
-
-
Save EgorBo/4529a4d30fb2e8c450eb693033a28dea to your computer and use it in GitHub Desktop.
ConstantField bug.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq.Expressions; | |
using System.Reflection; | |
namespace MyConsoleHelper | |
{ | |
class PropertyAndFields | |
{ | |
public string StringProperty { get; set; } | |
public string StringField; | |
public readonly string ReadonlyStringField; | |
public string ReadonlyStringProperty => ""; | |
public static string StaticStringProperty { get; set; } | |
public static string StaticStringField; | |
public static string StaticReadonlyStringProperty => ""; | |
public static readonly string StaticReadonlyStringField = ""; | |
public const string ConstantString = "Constant"; | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
MemberInfo member = typeof(PropertyAndFields).GetMember(nameof(PropertyAndFields.ConstantString))[0]; | |
Expression<Func<PropertyAndFields>> attemptAssignToConstant = Expression.Lambda<Func<PropertyAndFields>>( | |
Expression.MemberInit( | |
Expression.New(typeof(PropertyAndFields)), | |
Expression.Bind(member, Expression.Constant("")) | |
) | |
); | |
attemptAssignToConstant.Compile(true); | |
//netcore: System.NotSupportedException: Specified method is not supported. | |
//mono: System.MissingFieldException: Field 'MyConsoleHelper.PropertyAndFields.ConstantString' not found. | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment