将新 T 添加到空 List使用反射
我正在尝试使用反射将 Office 类的新实例添加到可能为空的列表中。
这些是我的课程
public class Report(){
public virtual ICollection<Officer> Officer { get; set; }
}
public class Officer(){
public string Name{ get; set; }
}
简化的代码片段:
Report report = new Report()
PropertyInfo propertyInfo = report.GetType().GetProperty("Officer");
object entity = propertyInfo.GetValue(report, null);
if (entity == null)
{
//Gets the inner type of the list - the Officer class
Type type = propertyInfo.PropertyType.GetGenericArguments()[0];
var listType = typeof(List<>);
var constructedListType = listType.MakeGenericType(type);
entity = Activator.CreateInstance(constructedListType);
}
//The entity is now List<Officer> and is either just created or contains a list of
//Officers
//I want to check how many officers are in the list and if there are none, insert one
//Pseudo code:
if (entity.count = 0)
{
entity.add(new instance of type)
}
非常感谢!
I'm attempting to set add a new instance of an Officer class to a potentially empty list using reflection.
These are my classes
public class Report(){
public virtual ICollection<Officer> Officer { get; set; }
}
public class Officer(){
public string Name{ get; set; }
}
Simplified code snippet:
Report report = new Report()
PropertyInfo propertyInfo = report.GetType().GetProperty("Officer");
object entity = propertyInfo.GetValue(report, null);
if (entity == null)
{
//Gets the inner type of the list - the Officer class
Type type = propertyInfo.PropertyType.GetGenericArguments()[0];
var listType = typeof(List<>);
var constructedListType = listType.MakeGenericType(type);
entity = Activator.CreateInstance(constructedListType);
}
//The entity is now List<Officer> and is either just created or contains a list of
//Officers
//I want to check how many officers are in the list and if there are none, insert one
//Pseudo code:
if (entity.count = 0)
{
entity.add(new instance of type)
}
Much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用:
Use:
您有两种选择:
1) 使用动态:
2) 使用反射:
You have two options:
1) Using dynamic:
2) Using Reflection:
这并不完全是您所要求的,但可以完成相同的任务。
如果
Officer
没有默认构造函数,那么您可以添加工厂回调This isn't quite what you asked for but may accomplish the same task.
If
Officer
doesn't have a default constructor then you could add a factory callback只需将您的实体适当地键入为
List
(或适当的更抽象类型(例如IList
))并正常使用即可:但无需检查是否插入与否,只需插入:
我假设(基于您已经知道如何使用反射创建实例的事实)您在创建
Officer
类型的实例时没有遇到问题。Just type your entity appropriately as a
List<Officer>
(or an appropriately more abstract type (such asIList
)) and use as normal:But no need to check whether to insert or not, just insert:
I'm assuming (based on the fact that you already know how to create instances using reflection) you're not having trouble creating the instance of type
Officer
.重新阅读您的问题后进行编辑:这并不能直接回答您的问题,而是建议采用不同的实现方式。
您可以轻松地在不使用反射的情况下完成:
然后调用例如:
您将获得一个包含 3 个 YourClass 实例(按其默认值)的列表
Edit after re-reading over your question: This doesn't directly answer your question but is rather a suggestion of a different implementation.
You can easily get by without using reflection:
Then calling e.g.:
you will have a list of 3 instances of YourClass by their default value