gpt4 book ai didi

c# - 使子对象有自己的类型静态容器

转载 作者:太空宇宙 更新时间:2023-11-03 19:46:29 25 4
gpt4 key购买 nike

是否可能每个类对象都有自己的静态数据存储?

我的意思是,只是为了执行以下操作:

class Program
{
static void Main(string[] args)
{
var car1 = new Car();
car1.Save(); ////saves in its own storage
var own1 = new Owner();
own1.Save(); //saves in its own storage as well
}
}

在我试过的代码中,出现这样的错误

'System.InvalidCastException`

在这个地方 var repo = (Repository<IEntity>) CurrentRepository;

出了什么问题,我该如何解决?

完整代码在这里:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
//var car1 = new Car();
//car1.Save(); ////saves in its own storage
//var own1 = new Owner();
//own1.Save(); //saves in its own storage as well var car1 = new Car();

}
}

public interface IEntity
{
long Id { get; }
}

public class Owner : Entity
{

public string FirstName { get; set; }

public string LastName { get; set; }

public Car Car { get; set; }

public Owner(string firstName, string lastName, Car car) : base(new Owner())
{
FirstName = firstName;
LastName = lastName;
Car = car;
}

public Owner() : base()
{
}

}

public class Car : Entity
{
public string Name { get; set; }
public int Year { get; set; }

public Car() : base()
{

}

public Car(string name, int year)
{
Name = name;
Year = year;
}
}

public abstract class Entity : IEntity
{
public long Id { get; }

public static object CurrentRepository { get; set; }

public Entity(Entity ent)
{
Type entityType = ent.GetType();
var instance = Activator.CreateInstance(typeof(Repository<>).MakeGenericType(entityType));

CurrentRepository = instance;

}

public Entity()
{
}

public void Save()
{
var repo = (Repository<IEntity>)CurrentRepository;
repo.Save(this);
}

public void Delete()
{
var repo = (Repository<IEntity>)CurrentRepository;
repo.Delete(this);
}
}

public interface IRepository<T> where T : IEntity
{
void Save(T entity);
void Delete(T entity);
T Find(long id);
}

public class Repository<T> : IRepository<T> where T : class, IEntity
{
protected BaseStorage<T> CustomDataStorage;

public Repository()
{
CustomDataStorage = new BaseStorage<T>();
}

public void Save(T entity)
{
CustomDataStorage.Add(entity);
}

public void Delete(T entity)
{
CustomDataStorage.Remove(entity);
}

public T Find(long id)
{
throw new NotImplementedException();
}
}
public class BaseStorage<T> : IStorage<T>
{
List<T> data = new List<T>();

public void Add(T entity)
{
data.Add(entity);
}

public void Remove(T entity)
{
throw new NotImplementedException();
}
}

public interface IStorage<T>
{
void Add(T entity);
void Remove(T entity);
}

}

最佳答案

In code I tried, I get such error

'System.InvalidCastException`

在这个地方 var repo = (Repository) CurrentRepository;

出了什么问题,我该如何解决?

那是因为你不能直接转换CurrentRepository , 类型 ( Repository<Car>Repository<Owner> ) 到 Repository<IEntity> .

参见 here有关此的更多信息...


要在这里实现你想要的,你可以这样尝试:

  1. 制作Entity泛型类(Entity<T>)并将泛型类型约束为IEntity
  2. 制作CurrentRepository一种 Repository<Entity<T>>
  3. 移动CurrentRepository的静态初始化从实例构造函数到静态构造函数。
  4. 制作OwnerCar Entity<T> 的对象子类与 T引用它自己的类型,使其成为自引用泛型

代码:

public class Owner : Entity<Owner>
{
...
}

public class Car : Entity<Car>
{
...
}

public abstract class Entity<T> : IEntity
where T: IEntity
{
public long Id { get; }

public static Repository<Entity<T>> CurrentRepository { get; set; }

static Entity()
{
CurrentRepository = new Repository<Entity<T>>();
}

public Entity()
{
}

public void Save()
{
CurrentRepository.Save(this);
}

public void Delete()
{
CurrentRepository.Delete(this);
}
}

关于c# - 使子对象有自己的类型静态容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44946602/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com