- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是 ITest 界面:
public interface ITest
{
Type ReturnType { get; }
Object MyMethod(params object[] args);
}
和测试类:
public class Test: ITest
{
public Type ReturnType { get { return typeof(long); } }
public Object MyMethod(params object[] args)
{
long sum = 0;
foreach(object arg in args)
{
sum += (long)arg;
}
return sum;
}
}
所以我需要一个方法来自动将 ITest
方法的结果转换为 ReturnType
类型。
我认为是这样的:
public T Transform<T>(Type T, object result)
{
return (T)result;
}
像这样使用:
Test test = new Test();
long result = Transform(test.ReturnType, test.MyMethod(1,2,3,4));
但如您所知,我不能像这样使用泛型方法,我不想像这样显式声明返回类型:
long result = Transform<long>(test.MyMethod(1,2,3,4));
有什么建议吗?
最佳答案
如果不进行反射(reflection),就不可能准确地提出您要问的问题。
您可以将 ITest 标记为 Generic
,从此一切都变得简单。
public interface ITest<T>
{
Type ReturnType { get; }//redundatnt
T MyMethod(params object[] args);
}
public class Test : ITest<long>
{
public Type ReturnType { get { return typeof(long); } }//redundatnt
public long MyMethod(params object[] args)
{
long sum = 0;
foreach (object arg in args)
{
long arg1 = Convert.ToInt64(arg);
sum += arg1;
}
return sum;
}
}
Test test = new Test();
long result = test.MyMethod(1,2,3,4);//No transform nothing, everything is clear
关于c# - 使用类型为 DataType 的泛型方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19466110/
这个问题已经有答案了: EmailAddress or DataType.Email attribute (3 个回答) 已关闭 3 年前。 我使用了像[EmailAddress]这样的数据属性和[D
我收到此错误: "CS0104: 'DataType' is an ambiguous reference between 'System.ComponentModel.DataAnnotations
1)我目前正在尝试理解以下代码,但我无法理解 void(*func)(void) 的含义,我可以理解我正在尝试保存名为“function”的函数的地址来自 list0513,在 void 指针函数处,
我们可以以某种方式应用这种格式吗? [Display(Name = "Date of Birthday")] [DataType(DataType.Date)] public DateTime DOB
我已按照 ORMLite 文档的规定将我的类型声明为 SERIALIZABLE,但我仍然得到: ORMLite can't store unknown class interface java.io.
嗨。 这就是我想要做的: str2 = "91"; str1 = "19"; var testQuery = from c1 in str1
我在 MVC 5 中使用 ASP.NET 身份。对于密码验证,我在 AccountViewModels.cs 中有以下代码: [Required] [DataType(DataType.Passwor
我试图了解使用之间的主要区别是什么[DataType(DataType.EmailAddress)] & [EmailAddress] . 在模型类中:- public class MYViewMod
使用 DataType Attribute 有什么区别?并传入值 DataType.Phone和 Phone Attribute它继承自 DataType 并自动设置 DataType.Phone?
我的 ViewModel 中有以下字段: [DataType(DataType.Date)] [Display(Name = "Preferred date)")] publi
如果没记错的话,是[DataType(DataType.Currency)]的DataAnnotation的对应部分在 Fluent api 中是 modelBuilder.Entity.Proper
给定一个 pandas.DataFrame带有包含混合数据类型的列,例如 df = pd.DataFrame({'mixed': [pd.Timestamp('2020-10-04'), 999, '
我在电子邮件字段上使用此代码: [Required] [DataType(DataType.EmailAddress)] [Display(Name = "Email addr
我是 NodeJs 开发的新手 我正在使用带有 mysql 和 Sequelize 的 NodeJs 来创建具有这些属性的 Batch 模型。 const Batch = sequelize.defi
我想动态生成我的数据框架构我有以下内容 错误: assert isinstance(dataType, DataType), "dataType should be DataType" Asse
我是 Golang 的新手。抱歉,我仍然对以下两者之间的区别感到困惑: type 和 type = 这是一个例子: package main import "fmt" func main()
当电子邮件地址以数字开头时,DataType DataAnnotation 中 DataType.Email 的内部验证似乎失败了。当具有 1234Something@gmail.com 样式电子邮件
有需求从CSV文件中提取数据,然后将其转换为XML,最终加载到目标中。 CSV文件中的数据为 Number,Email,Email Communication,Member Type,VIN 8532
我正在使用 [DataType(DataType.Date)]关于我们模型中的属性。 如何让 Razor 添加 css 类 datepicker到所有属于 DataType.Date 的文本框特性?
我的表 CalTime 有一个时间数据类型的列 Timespent。它以 HH:MM:SS 格式存储值。请建议如何求和该列的值。 Timespent _______ 00:07:
我是一名优秀的程序员,十分优秀!