Given an object, how can I tell if that object has virtual properties?
var entity = repository.GetByID(entityId);
I tried looking in:
PropertyInfo[] properties = entity.GetType().GetProperties();
But couldn't discern if any of the properties would indicate virtual.
PropertyInfo[] properties = entity.GetType().GetProperties()
.Where(p => p.GetMethod.IsVirtual).ToArray();
Or, for .NET 4 and below:
PropertyInfo[] properties = entity.GetType().GetProperties()
.Where(p => p.GetGetMethod().IsVirtual).ToArray();
That will get a list of public virtual properties.
It won't work for write-only properties. If it needs to, you can check CanRead
and CanWrite
manually, and read the appropriate method.
For example:
PropertyInfo[] properties = entity.GetType().GetProperties()
.Where(p => (p.CanRead ? p.GetMethod : p.SetMethod).IsVirtual).ToArray();
You could also just grab the first accessor:
PropertyInfo[] properties = entity.GetType().GetProperties()
.Where(p => p.GetAccessors()[0].IsVirtual).ToArray();
Checking only IsVirtual
of property's accessor will give you also interface properties that are not declared virtual
in your class. If by "virtual properties" you mean properties that you can override in derived class you should also check IsFinal
(sealed):
var accessor = typeof(MyType).GetProperty("MyProp").GetAccessors()[0];
var isVirtual = accessor.IsVirtual && ! accessor.IsFinal;
Check this sample app:
using System;
namespace VirtualPropertyReflection
{
interface I
{
int P1 { get; set; }
int P2 { get; set; }
}
class A : I
{
public int P1 { get; set; }
public virtual int P2 { get; set; }
static void Main()
{
var p1accessor = typeof(A).GetProperty("P1").GetAccessors()[0];
Console.WriteLine(p1accessor.IsVirtual); // True
Console.WriteLine(p1accessor.IsFinal); // True
var p2accessor = typeof(A).GetProperty("P2").GetAccessors()[0];
Console.WriteLine(p2accessor.IsVirtual); // True
Console.WriteLine(p2accessor.IsFinal); // False
}
}
}
See this answer.
try with
typeof(YourClass).GetProperty("YouProperty").GetGetMethod().IsVirtual;
Use the GetAccessors method, for example for the first property:
Get accessor:
properties[0].GetAccessors()[0].IsVirtual
Set accessor:
properties[0].GetAccessors()[1].IsVirtual
This is a little tricky, because a property can be read-only, write-only, or read/write. Therefore, you need to check both underlying methods for being virtual, like this:
PropertyInfo pi = ...
var isVirtual = (pi.CanRead && pi.GetMethod.IsVirtual)
|| (pi.CanWrite && pi.SetMethod.IsVirtual);