Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a property is virtual with reflection?

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.

like image 783
Travis J Avatar asked Sep 06 '12 18:09

Travis J


5 Answers

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();
like image 90
Kendall Frey Avatar answered Oct 24 '22 11:10

Kendall Frey


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.

like image 20
foka Avatar answered Oct 24 '22 12:10

foka


try with

typeof(YourClass).GetProperty("YouProperty").GetGetMethod().IsVirtual;
like image 8
Aghilas Yakoub Avatar answered Oct 24 '22 12:10

Aghilas Yakoub


Use the GetAccessors method, for example for the first property:

Get accessor:

properties[0].GetAccessors()[0].IsVirtual

Set accessor:

properties[0].GetAccessors()[1].IsVirtual
like image 5
Erwin Avatar answered Oct 24 '22 10:10

Erwin


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);
like image 3
Sergey Kalinichenko Avatar answered Oct 24 '22 11:10

Sergey Kalinichenko