mardi 21 mai 2019

How to override property CanWrite = false using reflection [duplicate]

This question already has an answer here:

I have a class with two properties and I want to override the read-only property (Name) using reflection.

This is the class

public class Test
{
    public int Id { get; set; }
    public string Name { get; }
}

This is how I attempt to override the property

var inst = new Test() { Id = 1 };
var prop = typeof(LogoutRequest).GetProperty("Name", BindingFlags.Public | BindingFlags.Instance);
prop.SetValue(inst, "a name");





Aucun commentaire:

Enregistrer un commentaire