I have a very simple class defined like:
public class Apple
{
public string MyString1;
public string MyString2;
public string MyString3;
}
When I try to retrieve the fields or properties of this type, or an instance of it, Reflection returns nothing. However, in Visual Studio, I can see the fields available in "DeclaredFields" member of the type.
Any idea what is going on here? The only thing I can think is maybe the namespaces are causing an issue. Both the location where I am trying to retrieve the property info and the location where the type is defined are in the same top-level namespace, but the former is using
the namespace of the latter.
Here is the reflection code:
public static class ReflectionHelper {
public
static
List<string>
GetPropertyNames(Type T)
{
try
{
var properties = T.GetProperties(BindingFlags.Public | BindingFlags.Instance);
if (properties != null)
{
return properties.ToList().Select(property => property.Name).ToList();
}
var fields = T.GetFields(BindingFlags.Public | BindingFlags.Instance);
if (fields != null)
{
return fields.ToList().Select(field => field.Name).ToList();
}
}
catch (Exception) { }
return null;
}
}
I am calling it like:
var apple = new Apple();
var stuff = ReflectionHelper.GetPropertyNames(typeof(Apple));
Aucun commentaire:
Enregistrer un commentaire