Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在LINQ表达式中动态创建从抽象基类继承的对象_C#_Linq_Generics_Abstract Class - Fatal编程技术网

C# 在LINQ表达式中动态创建从抽象基类继承的对象

C# 在LINQ表达式中动态创建从抽象基类继承的对象,c#,linq,generics,abstract-class,C#,Linq,Generics,Abstract Class,我有一个抽象类: public abstract class Entity<T> where T : Tweet { protected string _name; public abstract string Name { get; set; } public abstract List<Tweet> tweets { get; set; } } 不幸的是,由于Entity是一个抽象类,我无法创建它的实例

我有一个抽象类:

public abstract class Entity<T> where T : Tweet
    {
        protected string _name;
        public abstract string Name { get; set; }
        public abstract List<Tweet> tweets { get; set; }
    }
不幸的是,由于Entity是一个抽象类,我无法创建它的实例,因此如何在LINQ表达式中动态确定
o
实例的实际具体类。我有一个单独的方法,它基于对象类名的字符串表示来实现这一点,但我想知道如何使用LINQ实现这一点(下面的方法是我在应用程序中其他地方使用的实现):

私有静态实体createEntity(字符串类名、Tweet Tweet、字符串值)
{
实体=空;
Type t=Type.GetType(“FinalUniProject.NERModels.”+类名);
entity=(entity)Activator.CreateInstance(t);
实体名称=值;
//通过将tweet添加到名称列表,允许反向索引
如果(entity.tweets==null)entity.tweets=new List();
实体.tweets.Add(tweet);
返回实体;
}

下面的代码行肯定不会给出任何编译错误。没有检查所有方面。你确定这行不通吗

joined = list1.Union(list2)
                      .GroupBy(o => o.Name)
                      .Select(o =>
                      {
                          Entity<Tweet> entity = null;
                          string className = o.First().GetType().Name;
                          Type t = Type.GetType("FinalUniProject.NERModels." + className);
                          entity = (Entity<Tweet>)Activator.CreateInstance(t);
                          entity.Name = o.Key;
                          // Allow for inverted index by adding tweet to NamedEntist List<TweetModel>
                          entity.tweets = o.ToList().SelectMany(x => x.tweets).ToList();
                          return entity;
                      }).ToList();
joined=list1.Union(list2)
.GroupBy(o=>o.Name)
.选择(o=>
{
实体=空;
字符串className=o.First().GetType().Name;
Type t=Type.GetType(“FinalUniProject.NERModels.”+类名);
entity=(entity)Activator.CreateInstance(t);
entity.Name=o.Key;
//通过将tweet添加到名称列表,允许反向索引
entity.tweets=o.ToList().SelectMany(x=>x.tweets.ToList();
返回实体;
}).ToList();

在linq的
select
语句中插入
createEntity
函数的完整实现,并返回实体时有什么问题object@Jayant这将如何允许我仍然执行
。在
o
上选择many
,如对象内部所示初始值设定项?
Activator.CreateInstance(o.GetType())
,但最好使用抽象的
Clone
方法……听起来您需要一个类:JoinedWeetList或其他什么。
private static Entity<Tweet> createEntity(string className, Tweet tweet, string value)
{
    Entity<Tweet> entity = null;
    Type t = Type.GetType("FinalUniProject.NERModels." + className);
    entity = (Entity<Tweet>)Activator.CreateInstance(t);
    entity.Name = value;
    // Allow for inverted index by adding tweet to NamedEntist List<TweetModel>
    if (entity.tweets == null) entity.tweets = new List<Tweet>();
    entity.tweets.Add(tweet);
    return entity;
}
joined = list1.Union(list2)
                      .GroupBy(o => o.Name)
                      .Select(o =>
                      {
                          Entity<Tweet> entity = null;
                          string className = o.First().GetType().Name;
                          Type t = Type.GetType("FinalUniProject.NERModels." + className);
                          entity = (Entity<Tweet>)Activator.CreateInstance(t);
                          entity.Name = o.Key;
                          // Allow for inverted index by adding tweet to NamedEntist List<TweetModel>
                          entity.tweets = o.ToList().SelectMany(x => x.tweets).ToList();
                          return entity;
                      }).ToList();