英文:
How to automatically expand a type?
问题
我计划将实体类后代对象传递给一个函数,并调用相同函数的唯一实现,但我无法在没有强类型的情况下找出如何做到这一点。我希望对每种继承者的类型都有不同的行为。
public IRepository<Entity> choose(Entity entity)
{
return _repositories.FirstOrDefault(h => h.GetType().Namespace == entity.getStorePlace(((WeatherEntity)entity).getRealStorePlace));
}
public class Entity : IEntity
{
public string Id { get; set; }
public string getStorePlace(Func<string> f)
{
return f();
}
}
public class WeatherEntity : Entity, IweatherEntity
{
public string name { get; set; } = null!;
public int temperature { get; set; }
public string location { get; set; } = null!;
public bool isRainyToday { get; set; } = false;
public string getRealStorePlace()
{
if (temperature > 25)
{
return "ConsoleAppTest.Repositories.MongoRepositories";
}
return "ConsoleAppTest.Repositories.JsonRepositories";
}
}
我尝试在实体类中添加唯一函数,并在WeatherEntity类中进行覆盖,但结果相同。
英文:
I plan to pass Entity-class descendant objects to a function and call a unique implementation of the same function, but I can't figure out how to do it without strong typing. I want to have different behavior for each type of inheritor.
public IRepository<Entity> choose(Entity entity)
{
return _repositories.FirstOrDefault(h => h.GetType().Namespace == entity.getStorePlace(((WeatherEntity)entity).getRealStorePlace));
}
public class Entity : IEntity
{
public string Id { get; set; }
public string getStorePlace(Func<string> f)
{
return f();
}
}
public class WeatherEntity : Entity, IweatherEntity
{
public string name { get; set; } = null!;
public int temperature { get; set; }
public string location { get; set; } = null!;
public bool isRainyToday { get; set; } = false;
public string getRealStorePlace()
{
if (temperature > 25)
{
return "ConsoleAppTest.Repositories.MongoRepositories";
}
return "ConsoleAppTest.Repositories.JsonRepositories";
}
}
I tried to add unique function to the Entity class and override it in WeatherEntity Class, but got the same result.
答案1
得分: 0
我将以下部分翻译为中文:
我将 Entity 类更改为抽象类,现在它正常工作。
public IRepository<Entity> choose(Entity entity)
{
return _repositories.FirstOrDefault(h => h.GetType().Namespace == entity.getStorePlace(entity.getRealStorePlace));
}
public abstract class Entity : IEntity
{
public string Id { get; set; }
public string getStorePlace(Func<string> f)
{
return f();
}
public abstract string getRealStorePlace();
}
public class WeatherEntity : Entity, IweatherEntity
{
public string name { get; set; } = null!;
public int temperature { get; set; }
public string location { get; set; } = null!;
public bool isRainyToday { get; set; } = false;
public override string getRealStorePlace()
{
if (temperature > 25)
{
return "ConsoleAppTest.Repositories.MongoRepositories";
}
return "ConsoleAppTest.Repositories.JsonRepositories";
}
}
请注意,我已将代码中的 HTML 实体代码(如 <
、>
、"
)还原为正常的代码。
英文:
I changed Entity class to abstracted and it workes now.
public IRepository<Entity> choose(Entity entity)
{
return _repositories.FirstOrDefault(h => h.GetType().Namespace == entity.getStorePlace(entity.getRealStorePlace));
}
public abstract class Entity : IEntity
{
public string Id { get; set; }
public string getStorePlace(Func<string> f)
{
return f();
}
public abstract string getRealStorePlace();
}
public class WeatherEntity : Entity, IweatherEntity
{
public string name { get; set; } = null!;
public int temperature { get; set; }
public string location { get; set; } = null!;
public bool isRainyToday { get; set; } = false;
public override string getRealStorePlace()
{
if (temperature > 25)
{
return "ConsoleAppTest.Repositories.MongoRepositories";
}
return "ConsoleAppTest.Repositories.JsonRepositories";
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论