Tag: reflection

c#中构造函数参数的名称

我有一个要求,我需要在我的类中获取构造函数的变量名称。 我尝试使用c#reflection,但constructorinfo没有提供足够的信息。 因为它只提供参数的数据类型,但我想要名称,例如 class a { public a(int iArg, string strArg) { } } 现在我想要“iArg”和“strArg” 谢谢

如何从C#(2.0)中的属性获取属性的名称

我知道我可以有一个属性,但这比我想去的工作更多……而且不够通用。 我想做点什么 class Whotsit { private string testProp = “thingy”; public string TestProp { get { return testProp; } set { testProp = value; } } } … Whotsit whotsit = new Whotsit(); string value = GetName(whotsit.TestProp); //precise syntax up for grabs.. 在哪里我期望价值等于“TestProp” 但我不能为我的生活找到正确的reflection方法来编写GetName方法… 编辑:我为什么要这样做? 我有一个类来存储从’name’,’value’表中读取的设置。 这由基于reflection的通用方法填充。 我非常想反过来写… /// /// Populates an object from a […]

通过传递名称来获取和设置字段值

我在类中有一个字段,其随机名称如下: class Foo { public string a2de = “e2” } 我在另一个变量中有这个字段的名称,如: string vari = “a2de” 我可以使用a2de的值来获取或设置字段a2de的值吗? 喜欢: getvar(vari) 要么 setvar(vari) = “e3”

使用reflection的通用列表

我有一个场景,我将Class name as a string我只想创建该类的通用List 。 我尝试了以下内容 Type _type = Type.GetType(className); List list = new List(); 但是我觉得error 。 我不能把那个_type作为列表参数。 如果有人知道请帮助我

获取Type中使用的程序集路径

我需要一个方法,它接受一个Type并返回该类型中使用的所有程序集的路径。 我写了这个: public static IEnumerable GetReferencesAssembliesPaths(this Type type) { yield return type.Assembly.Location; foreach (AssemblyName assemblyName in type.Assembly.GetReferencedAssemblies()) { yield return Assembly.Load(assemblyName).Location; } } 通常这种方法可以胜任,但也有一些缺点: 我没有找到如何从类型本身获取引用的程序集/类型,所以我使用了type.Assembly.GetReferencedAssemblies()并获得了整个程序集的引用,而不仅仅是那些与类型相关的引用。 type.Assembly.GetReferencedAssemblies()返回AssemblyName,没有location / path / filepath属性。 要获取location属性,我首先使用Assembly.Load()然后使用location属性。 我不希望加载程序集获取它们的路径,因为它们没有必要使用,并且因为Assembly.Load()可能会因FileNotFoundException或BadImageFormatException而失败。

从方法引用C#获取methodinfo

当我们想要获取指定类型的Type实例时,我们可以使用C# typeof关键字。 但是,如果我想通过它的引用获取方法的MethodInfo ,我可以使用什么? 例如,我有一个简单的控制台应用程序。 它包含Program.Main方法。 我想通过使用methodinfoof(Program.Main)类的东西获得MethodInfo 。 我有这个问题,因为方法名称可能会更改,所以我不能只使用Type.GetMethodInfo(string MethodName) 。 我有大约10000个方法,我想获得MethodInfo ,所以在我的方法中添加任何自定义属性或其他任何东西都不是解决方案。

如何validation类型是否重载/支持某个运算符?

如何检查某种类型是否实现某个运算符? struct CustomOperatorsClass { public int Value { get; private set; } public CustomOperatorsClass( int value ) : this() { Value = value; } static public CustomOperatorsClass operator +( CustomOperatorsClass a, CustomOperatorsClass b ) { return new CustomOperatorsClass( a.Value + b.Value ); } } 以下两次检查应该返回true : typeof( CustomOperatorsClass ).HasOperator( Operator.Addition ) typeof( int ).HasOperator( Operator.Addition […]

如何使用reflection获取类的所有静态属性及其值

我有一个这样的课: public class tbl050701_1391_Fields { public static readonly string StateName = “State Name”; public static readonly string StateCode = “State Code”; public static readonly string AreaName = “Area Name”; public static readonly string AreaCode = “Area Code”; public static readonly string Dore = “Period”; public static readonly string Year = “Year”; } 我想写一些语句,返回一个包含以下值的Dictionary : Key […]

给定“where T:new()”,“new T()”在内部使用Activator.CreateInstance吗?

如果我有一个类型参数约束new() : void Foo() where T : new() { var t = new T(); } new T()会在内部使用Activator.CreateInstance方法(即reflection)吗?

如何在System.Reflection中识别匿名方法

如何通过reflection识别匿名方法?