从对象中获取 属性 名称和它期望的类型
Get property name and the type it expects from an object
如何获取所有 属性 名称和它期望从对象获得的值类型?
假设我有这 2 个实体 classes:
public class Blog
{
public int Id { get; set; }
public string Title { get; set; }
public string BloggerName { get; set;}
public Post Post { get; set; }
}
public class Post
{
public int Id { get; set; }
public string Title { get; set; }
public DateTime DateCreated { get; set; }
public string Content { get; set; }
public int BlogId { get; set; }
public Comment Comment { get; set; }
}
我怎样才能得到这个结果:
- "Blog.Id expects an int"
- "Blog.Title expects a string"
- "Blog.BloggerName expects a string"
- "Post.Id expects an int"
- "Post.Title expects a string"
- "Post.DateCreated expects a string"
- 等...
我知道这可以一次完成一个 属性,但是有没有更优雅的方法来做到这一点,因为实体 class 有很多属性(并且它们随着它的类型而改变仍在开发中)并且有复杂的对象我想做同样的事情?
编辑,这需要递归完成。只是传递 Blog
而不知道它是否包含另一个用户定义的对象,如 Post
,这不是我要找的。
当然要用到反射。
foreach (PropertyInfo p in typeof(Blog).GetProperties())
{
string propName = p.PropertyType.Name;
Console.WriteLine("Property {0} expects {1} {2}",
p.Name,
"aeiou".Contains(char.ToLower(propName[0])) ? "an" : "a",
propName);
}
请注意 GetProperties
also has an overload which accepts a BindingFlags
,它只允许您获取某些属性,例如instance/static public/private.
下面是一个理论上如何递归工作的示例,尽管即使在这个简单的示例中,这也会创建一个 WhosebugException
,因为 DateTime
具有本身为 DateTime
的属性.
void ListProperties(Type t)
{
foreach (PropertyInfo p in t.GetProperties())
{
string propName = p.PropertyType.Name;
Console.WriteLine("Property {0} expects {1} {2}",
p.Name,
"aeiou".Contains(char.ToLower(propName[0])) ? "an" : "a",
propName);
ListProperties(p.PropertyType);
}
}
ListProperties(typeof(Blog));
您可以为此使用反射并执行如下操作:
public static class Extensions
{
public static void PrintAllProperties<T>(T type)
{
var t = type.GetType();
var properties = t.GetProperties();
Console.WriteLine("Listing all properties for type {0}", t);
foreach (var prop in properties)
{
Console.WriteLine("{0} is of type: {1}", prop.Name, prop.PropertyType);
}
}
}
然后使用:
Extensions.PrintAllProperties(new Blog());
Extensions.PrintAllProperties(new Post());
如何获取所有 属性 名称和它期望从对象获得的值类型?
假设我有这 2 个实体 classes:
public class Blog
{
public int Id { get; set; }
public string Title { get; set; }
public string BloggerName { get; set;}
public Post Post { get; set; }
}
public class Post
{
public int Id { get; set; }
public string Title { get; set; }
public DateTime DateCreated { get; set; }
public string Content { get; set; }
public int BlogId { get; set; }
public Comment Comment { get; set; }
}
我怎样才能得到这个结果:
- "Blog.Id expects an int"
- "Blog.Title expects a string"
- "Blog.BloggerName expects a string"
- "Post.Id expects an int"
- "Post.Title expects a string"
- "Post.DateCreated expects a string"
- 等...
我知道这可以一次完成一个 属性,但是有没有更优雅的方法来做到这一点,因为实体 class 有很多属性(并且它们随着它的类型而改变仍在开发中)并且有复杂的对象我想做同样的事情?
编辑,这需要递归完成。只是传递 Blog
而不知道它是否包含另一个用户定义的对象,如 Post
,这不是我要找的。
当然要用到反射。
foreach (PropertyInfo p in typeof(Blog).GetProperties())
{
string propName = p.PropertyType.Name;
Console.WriteLine("Property {0} expects {1} {2}",
p.Name,
"aeiou".Contains(char.ToLower(propName[0])) ? "an" : "a",
propName);
}
请注意 GetProperties
also has an overload which accepts a BindingFlags
,它只允许您获取某些属性,例如instance/static public/private.
下面是一个理论上如何递归工作的示例,尽管即使在这个简单的示例中,这也会创建一个 WhosebugException
,因为 DateTime
具有本身为 DateTime
的属性.
void ListProperties(Type t)
{
foreach (PropertyInfo p in t.GetProperties())
{
string propName = p.PropertyType.Name;
Console.WriteLine("Property {0} expects {1} {2}",
p.Name,
"aeiou".Contains(char.ToLower(propName[0])) ? "an" : "a",
propName);
ListProperties(p.PropertyType);
}
}
ListProperties(typeof(Blog));
您可以为此使用反射并执行如下操作:
public static class Extensions
{
public static void PrintAllProperties<T>(T type)
{
var t = type.GetType();
var properties = t.GetProperties();
Console.WriteLine("Listing all properties for type {0}", t);
foreach (var prop in properties)
{
Console.WriteLine("{0} is of type: {1}", prop.Name, prop.PropertyType);
}
}
}
然后使用:
Extensions.PrintAllProperties(new Blog());
Extensions.PrintAllProperties(new Post());