- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
由于某些要求,我应该将一个类转换为通用接口(interface)。但是我收到 ReSharper 警告“可疑类型转换,解决方案中没有从两者继承的类型”。在运行时,它会导致运行时错误。
我应该这样做,因为我正在使用反射,所以我非常需要这个类型转换。
我的代码如下。任何支持表示赞赏。
class Program
{
static void Main(string[] args)
{
// prepare class name from string
string propertyNavigationFullName = "ConsoleApp1.Entry";
// Take assembly
Assembly assem = typeof(Entry).Assembly;
// take class type
Type className = assem.GetType(propertyNavigationFullName, true);
// create class instance
IEntitySubMultiLang inst = (IEntitySubMultiLang)Activator.CreateInstance(className);
// prepare Generic Class
Type genericClass = typeof(GenericRepo<>);
Type constructedClass = genericClass.MakeGenericType(className);
// create Generic Class instance
// ERROR OCCUR HERE
IRepo<IEntitySubMultiLang> created = (IRepo<IEntitySubMultiLang>)Activator.CreateInstance(constructedClass);
// I need to use Add method or any other method
created.Add(inst);
}
public interface IEntitySubMultiLang
{
int Id { get; set; }
}
public interface IRepo<TEntity> where TEntity : class, IEntitySubMultiLang
{
void Add(TEntity item);
}
public class GenericRepo<TEntity> : IRepo<TEntity> where TEntity : class, IEntitySubMultiLang
{
public void Add(TEntity item)
{
}
}
public class Entry : IEntitySubMultiLang
{
public int Id { get; set; }
}
}
最佳答案
如果您考虑一下这意味着什么,您所谈论的类型转换实际上是不正确的,这就是它不起作用的原因。假设您有另一个实现 IEntitySubMultiLang 的类:
public class Foo : IEntitySubMultiLang
{
public int Id { get; set; }
}
Foo
会发生什么?至
_r.Add()
?
_r.Add(new Foo());
_r
是
GenericRepo<Entry>
,不知道怎么加
Foo
s。这正是安全应该避免的那种情况类型。
_r.Add()
时,您实际上并没有从接口(interface)的类型安全方面获得编译时优势。 .所以接受这个事实,并使用反射来调用
Add
方法,而不尝试进行任何转换。
constructedClass
.GetMethod("Add")
.MakeGenericMethod(className)
.Invoke(created, new object[] { inst });
static private void AddToRepo<TEntity>() where TEntity : class, IEntitySubMultiLang, new
{
new GenericRepo<TEntity>().Add(new TEntity());
}
typeof(ConsoleApp1)
.GetMethod("AddToRepo")
.MakeGenericMethod(className)
.Invoke(null, new object[] { /* any parameters? */});
关于c# - 可疑类型转换解决方案中没有继承自两者的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59199163/
继this q about GADTs之后,我正在尝试构建一个 EDSL(对于本文中的示例),但没有 GADT。我已经找到了一些可以避免 AST 数据类型加倍的方法;但相反,它似乎将代码加倍。所以我尝
我正在使用 compose 实现 MVI。为了让我遵循正确的事件循环,我需要通过我的 View 模型传播点击事件,然后观察副作用。我看过一些实现,它们都使用 LaunchedEffect(true)
有没有办法使用Python对日志文件中的“可疑时间”进行有效的二进制搜索? 我有一个日志文件,其条目如下: 02:38:18 0 RcvTxData - 11 : Telegram receive
一些黑客可以访问我的数据库,我将找出漏洞;我无权访问服务器日志。我制作了这段代码来保存文本文件中的可疑查询: function query($query) { $file = 'sqllog.txt'
问题背景: 日常对Sql Server 2005关系数据库进行操作时,有时对数据库(如:Sharepoint网站配置数据库名Sharepoint_Config)进行些不正常操作如数据库在读写时而无
1、出错情况 有些时候当你重启了数据库服务,会发现有些数据库变成了正在恢复、置疑、可疑等情况,这个时候DBA就会很紧张了,下面是一些在实践中得到证明的方法。 在一次重启数据
我在尝试解决这段短代码中的运行时段错误时遇到了麻烦。我怀疑这与代码中使用 system() 和 strcpy() 有关,但由于我没有遇到此类错误的经验,所以我不确定该怎么做,而且到目前为止我还没有找到
我正在尝试按照 NutchTutorial 运行基本爬网: bin/nutch crawl urls -dir crawl -depth 3 -topN 5 所以我已经安装了 Nutch,并使用 So
我注意到,当我通过浏览器打开 CSS 时,我看到我所有的图像都预先设置了一些 IP。它看起来像这样: url(http://64.19.142.11/www.mydomain.com/i/pgBg.p
我是一名优秀的程序员,十分优秀!