Using Propertyinfo to Find Out the Property Type

Using PropertyInfo to find out the property type

Use PropertyInfo.PropertyType to get the type of the property.

public bool ValidateData(object data)
{
foreach (PropertyInfo propertyInfo in data.GetType().GetProperties())
{
if (propertyInfo.PropertyType == typeof(string))
{
string value = propertyInfo.GetValue(data, null);

if value is not OK
{
return false;
}
}
}

return true;
}

Getting property type with reflection

You could use PropertyType to determine which is string or decimal. Try like this;

Type t = obj.GetType();
PropertyInfo propInfo = t.GetProperty("article");
if (propInfo.PropertyType == typeof(string))
{
Console.WriteLine("String Type");
}
if (propInfo.PropertyType == typeof(decimal)
|| propInfo.PropertyType == typeof(decimal?))
{
Console.WriteLine("Decimal Type");
}

C# check if type of PropertyInfo is primitive

The main reason is that whenever I do pi.GetType() it says that it's a PropertyInfo.

You should use PropertyType property of PropertyInfo instead of using GetType() method.

Excerpt from documentation:

Gets the type of this property.

So instead of

pi.GetType().IsPrimitive 

use this

pi.PropertyType.IsPrimitive 

How can I know if a propertyInfo is of the type IList in C#?

You can use GetGenericTypeDefinition

if(properties[i].PropertyType.IsGenericType &&
properties[i].PropertyType.GetGenericTypeDefinition() == typeof(IList<>))

This will return true for all IList<> types.If you wanna check for others, (ICollection, IEnumerable etc.) you can do the same check for them as well.

What is the difference between propertyInfo.PropertyType and propertyInfo.GetType?

Well, propertyInfo.PropertyType is the type of the property that the propertyInfo object represents. It is typeof(List<string>) in your case. On the other hand, propertyInfo.GetType() is a type derived from typeof(PropertyInfo).

How can I determine property types using reflection?

What type are you interested in? The return type of the method/property/event etc?

If so, I don't think there's anything in MemberInfo to let you get at it directly - you'll need to cast and use MethodInfo.ReturnType, PropertyInfo.PropertyType, FieldInfo.FieldType, EventInfo.EventHandlerType and any others I've forgotten. (Remember that types themselves can be members. Not sure what you'll want to do with them!)

EDIT: If you're interested in whether a specific Type represents either MyType or some subclass, then use Type.IsAssignableFrom:

if (typeof(MyType).IsAssignableFrom(type))

EDIT: Now that we know you want properties, it's easy - use GetProperties instead of GetMembers. I like doing reflection with LINQ:

var query = from type in assembly.GetTypes()
from property in type.GetProperties()
where typeof(MyType).IsAssignableFrom(property.PropertyType)
select new { Type=type, Property=property };

foreach (var entry in query)
{
Console.WriteLine(entry);
}

If you're not a fan of LINQ:

foreach (Type t in a.GetTypes())
foreach (PropertyInfo pi in t.GetProperties())
if (typeof(MyType).IsAssignableFrom(pi.PropertyType))
Console.WriteLine("Found a property that is MyType");

Note that you might want to specify binding flags to get non-public properties etc.

Getting the type of a class property c#

In your class you have properties like

public int? Id { get; set; }

But in your switch you are checking this case

case Type _ when propertyInfo.PropertyType == typeof(int):

and the problem is that typeof(int?) != typeof(int)

You need to add cases for your nullable types.

How to know the Type of a Property if it is collection in Reflection?

Use propertyInfo.PropertyType instead of propertyInfo.GetValue(obj,null).GetType() which should give you the property type even if the property value is null.

So when you have a class like

public class Foo {
public List<string> MyProperty { get; set; }
}

and an instance of Foo in obj, then

var propertyInfo = obj.GetType().GetProperty("MyProperty"); // or find it in a loop like in your own example
var typeArg = propertyInfo.PropertyType.GetGenericArguments()[0];

will give you the value System.String (as a System.Type instance) in typeArg.



Related Topics



Leave a reply



Submit