Skip to content

Instantly share code, notes, and snippets.

@EgorBo
Created September 8, 2017 13:49
Show Gist options
  • Save EgorBo/4529a4d30fb2e8c450eb693033a28dea to your computer and use it in GitHub Desktop.
Save EgorBo/4529a4d30fb2e8c450eb693033a28dea to your computer and use it in GitHub Desktop.
ConstantField bug.cs
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