- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试通过为 Getter
创建委托(delegate)来改进我的反射代码和 Setter
方法。
我的代码是这样的:
MyObject obj = new MyObject();
var prop = obj.GetType().GetProperty("Prop");
var getType = typeof(Func<>).MakeGenericType(prop.PropertyType);
var setType = typeof(Action<>).MakeGenericType(prop.PropertyType);
var getMethod = prop.GetGetMethod().CreateDelegate(getType, obj);
var setMethod = prop.GetSetMethod().CreateDelegate(setType, obj);
// I'd like to change this section and not to use a dynamic!!
dynamic castedGet = Convert.ChangeType(getMethod, getType);
dynamic castedSet = Convert.ChangeType(setMethod, setType);
CreateDelegate
返回 Delegate
并使用 DynamicInvoke
不是性能方面的。
我类型转换(硬编码)了 Delegate
进入Action<T> \ Func<T>
并看到我的表现有了巨大的提高。
然后我尝试转换 Delegate
进入Action<T> \ Func<T>
在运行时(使用 Convert.ChangeType
和 dynamic
)我的表现受到了伤害——可能是因为我使用的是 dynamic
类型。
我很确定我可以在没有 dynamic
的情况下做到这一点.
我猜想解决方案与expression trees
有关,但我不太确定如何编写这样的代码。如果有人有不使用的好解决方案 expression trees
听到它也会很有趣。
最佳答案
如果您的目标是能够在编译时不知道返回类型的情况下调用您的操作/函数,那么您可能希望以 Action<object>
结束。和 Func<object>
,对吧?
您无需编译表达式树或其他任何东西即可执行此操作,如下所示:
// Use reflection to create the action, invoking the method below.
var setAction = (Action<object>) this.GetType()
.GetMethod("CastAction", BindingFlags.Static | BindingFlags.NonPublic)
.MakeGenericMethod(prop.PropertyType)
.Invoke(null, new object[]{setMethod});
// invoke the action like this:
object value = 42; // or any value of the right type.
setAction(value);
使用这个辅助方法:
private static Action<object> CastAction<T>(Delegate d)
{
var action = (Action<T>)d;
return obj => action((T)obj);
}
我的测试表明这比使用 dynamic
快大约 25% ,比只说 obj.Prop = 2
慢了大约 45% ;
关于c# - 在运行时将委托(delegate)转换为 Action<T> 或 Func<T>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32698305/
我对 Swift 比较陌生。我在 pickerView 中使用 PFQuery 时遇到了问题。 我正在尝试实现一个 2 组件 pickerView,如下所示: 组件 0:组件 1 “A”:“Altiv
packageUrl="http://192.168.0.112" packageUrl_line=`grep -n "packageUrl" ${file} | head -1 | cut -d "
我遇到了一个烦人的问题。我正在做一个消息传递应用程序。在消息页面中,当用户滚动到顶部时,一旦最上面的单元格可见,它将加载更多消息以显示。因为在加载更多消息时, TableView 的位置在顶部(内容偏
可以屏蔽吗 Jsoup.connect("http://xyz.com").get().html(); 作为对网站的浏览器调用? 我尝试构建一个壁纸下载工具,但在从服务器下载页面时遇到问题。 如果我下
IntelliJ 给了我以下代码的提示: val l = List(0, "1", 2, "3") l.foreach{_ match {case xx:Int => println(xx);case
我正在尝试读取使用三个水平点表示缺失值的 excel 文件,例如... https://population.un.org/wpp/Download/Files/1_Indicators%20(Sta
我有一个 NSPopupButton,其内容绑定(bind)到 NSArray,假设该数组是 @[ @"Option 1", @"Option 2" ]; 其选择的对象绑定(bind)
大家好,我希望您能回答这个问题:) 基本上,我的index.html中有一些html 5视频,并且会自动播放背景音频。 我想发生的是在播放视频标签时将背景音频减少到50%。如果可能的话,如果我可以使用
需要一点帮助来解决这个问题。我的目标是拥有一个可执行的 jar 文件,它可以截取网页的屏幕截图,并且可以在 Windows 和 Linux 机器上运行。我尝试过使用 html2image 但 phan
我知道如何将 comboBox 值插入到 sql 中,但不知道如何在 sql 中用数字替换 comboBox 值。 这是我的组合框编码和处理按钮的一部分。 用户.java JComboBox com
我有一个像这样的 XML 片段: WEL SMIO 01/01/2015 12/31/9999 AAE 01/01/2015
好的,我知道每个人都认为 IFrame 不好,我知道这一点。但我“被要求”在极少数情况下使用一个。 所以我的问题是,当您有一个包含 IFrame 的 .aspx 页面,然后在该 IFrame 中有另一
我有一个 React 应用程序,我在其中使用 axios 库来获取一些值,并将它们设置为处于我状态的 javascript 对象数组 componentDidMount(){ axios.
我必须存储我的 HashMap通过Spring data导入数据库MySql。为了从 HashMap 检索数据,我使用键值,并且可能会发生键不存在的情况,在这种情况下,我必须避免使用 set 方法将值
我有 Size 模型,其中 value 作为字符串。我想根据 value 属性通过将其转换为十进制来排序 size。 has_many :sizes, -> {order 'value ASC'},这
嗨,我有一个 MYSQL 表,例如 PART{ part_id :long,Auto Increment parent_part_id :long root_part_id :long } 我需
我正在尝试将列名为“邮政编码”、“2010 年人口”、“Land-Sq-Mi”和“每平方英里密度”的 CSV 文件导入我的测试表,该表名为 derp--这就是我在开头使用 drop 语句的原因,这样我
我想写一个这样的存储过程: CREATE OR REPLACE FUNCTION my_function(param_1 text, param_2 text DEFAULT NULL::text)
我有一个 RecyclerView,在它之上,有一个 AdView。滚动 RecyclerView 时,我想将 Adview 留在固定位置。我该怎么做? 这是我打开应用程序时的 RecyclerVie
我有一个 UIScrollView,其中包含一个 UIView 容器,该容器包含多个 UITextField。 我想执行以下操作:如果我选择一个字段并且它位于键盘下方,则将文本字段提升到键盘上方 20
我是一名优秀的程序员,十分优秀!