30

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.

6 Answers 6

47
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();
1
  • To safely handle private properties you can do: (p.CanRead ? p.GetGetMethod(true) : p.GetSetMethod(true)).IsVirtual; Commented Jul 16, 2014 at 0:24
23

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.

1
  • 4
    Thanks for this, lots of other answers talked about isVirtual but the combined isFinal check did it for me.
    – webnoob
    Commented Jan 4, 2017 at 10:17
8

try with

typeof(YourClass).GetProperty("YouProperty").GetGetMethod().IsVirtual;
5

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

Get accessor:

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

Set accessor:

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

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);
0

IsVirtual alone didn't work for me. It was telling me that all my non-virtual non-nullable properties were virtual. I had to use a combination of IsFinal and IsVirtual

Here's what I ended up with:

PropertyInfo[] nonVirtualProperties = myType.GetProperties().Where(x => x.GetAccessors()[0].IsFinal || !x.GetAccessors()[0].IsVirtual).ToArray();

PropertyInfo[] virtualProperties = myType.GetProperties().Where(x => !x.GetAccessors()[0].IsFinal && x.GetAccessors()[0].IsVirtual).ToArray();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.