Getting the access set for a property in an abstract class is not possible?

Given the class':

public abstract class AbstractEntity { public virtual Guid Id { get; private set; } } public class Entity { public virtual Guid Id { get; private set; } } 

And the Info property for the Id property.

When calling the method:

 PropertyInfo.GetAccessors() 

It returns both the get method and the set method when the class is not abstract (Entity), but only the get method when the class is abstract (AbstractEntity).

Why is this? And is there another way to get the set method from a property with a private set?

+4
source share
2 answers

If you want to get MethodInfo for a set, you can. This does not mean that you can use it, as Kevin points out in his answer.

 Type t = typeof(AbstractEntity); MethodInfo[] mi = t.GetProperty("Id").GetAccessors(true); 
+2
source

In an abstract class, you cannot create it. Prohibiting reflection, there is nothing that could cause a private setter. In reflection, you still need to create an instance of the class (not including static elements) to access methods for invoking properties, etc., And this cannot be done in an abstract class. Being able to access it will not provide you with anything, and in fact nothing can gain access to it in order to use it.

+1
source

Source: https://habr.com/ru/post/1309987/


All Articles