gpt4 book ai didi

.net - 访问 { get set } 属性方法,无需反射

转载 作者:行者123 更新时间:2023-12-01 10:27:29 25 4
gpt4 key购买 nike

我希望能够访问属性的 void set_PropertyName(var value)var get_Property() 方法。

有什么方法可以反射吗?而且没有得到 Compiler Error CS0571 .

编辑没有反射我的意思是没有反射所做的开销(不仅仅是必须编写代码),因为这是需要运行很多次的代码我希望它没有反射的开销。不幸的是,dynamic 也在幕后使用了反射。

似乎这对于 c++.net 可能是可行的,但对于其他语言则不行(虽然我不能说,因为我的 C++ 安装被塞满了)。

感谢您的回复。

最佳答案

是的,如果您使用 callbyname 是可能的可从 Microsoft.VisualBasic 命名空间获得

例如;

using System;
using Microsoft.VisualBasic;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var test = new Test();
Microsoft.VisualBasic.Interaction.CallByName(test, "property" ,CallType.Set, 1);
Console.WriteLine(Microsoft.VisualBasic.Interaction.CallByName(test, "property", CallType.Get));
Console.ReadKey(true);
}
}

public class Test
{
public int property { get; set; }
}
}

关于.net - 访问 { get set } 属性方法,无需反射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19530881/

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