Travis J
Travis J

Reputation: 82267

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.

Upvotes: 30

Views: 14636

Answers (6)

Owen
Owen

Reputation: 4397

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

Upvotes: 0

foka
foka

Reputation: 860

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.

Upvotes: 23

Kendall Frey
Kendall Frey

Reputation: 44316

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

Upvotes: 47

Erwin
Erwin

Reputation: 4817

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

Get accessor:

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

Set accessor:

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

Upvotes: 5

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

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

Upvotes: 3

Aghilas Yakoub
Aghilas Yakoub

Reputation: 28970

try with

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

Upvotes: 8

Related Questions