Tag: reflection

.NET Reflection设置私有属性

如果您有一个如下定义的属性: private DateTime modifiedOn; public DateTime ModifiedOn { get { return modifiedOn; } } 如何使用Reflection将其设置为某个值? 我试过了两个: dto.GetType().GetProperty(“ModifiedOn”).SetValue(dto, modifiedOn, null); 和 dto.GetType().GetProperty(“modifiedOn”).SetValue(dto, modifiedOn, null); 但没有任何成功。 很抱歉,如果这是一个愚蠢的问题,但这是我第一次使用Reflection with C#.NET。

是否有可能通过反思得到一个房产的私人制定者?

我写了一个自定义序列化程序,通过reflection设置对象属性。 可序列化类用serializable属性标记,所有可序列化属性也被标记。 例如,以下类是可序列化的: [Serializable] public class Foo { [SerializableProperty] public string SomethingSerializable {get; set;} public string SometthingNotSerializable {get; set;} } 当系列化程序被要求反序列化SomethingSerializable ,它获取属性的set方法并使用它来设置它,如下所示: PropertyInfo propertyInfo; //the property info of the property to set //…// if (propertyInfo.CanWrite && propertyInfo.GetSetMethod() != null) { propertyInfo.GetSetMethod().Invoke(obj, new object[]{val}); } 这工作正常,但是,如何才能使属性设置器只对序列化器可访问? 如果setter是私有的: public string SomethingSerializable {get; private set;} 然后对propertyInfo.GetSetMethod()的调用在序列化程序中返回null。 有没有办法访问私有的setter或任何其他方式,以确保只有序列化程序可以访问setter? 不保证序列化程序在同一个程序集中。

使用C#中的名称调用方法

我的应用程序中有许多“作业”,每个作业都有一个需要调用的方法列表及其参数。 基本上称为包含以下对象的列表: string Name; List Parameters; 所以基本上,当一个作业运行时我想通过这个列表进行枚举,并调用相关的方法。 例如,如果我有如下方法: TestMethod(string param1, int param2) 我的方法对象是这样的: Name = TestMethod Parameters = “astring”, 3 是否有可能做到这一点? 我想reflection将成为关键。

使用自定义属性获取程序集中的所有类型

是否有一种优雅的方法来获取具有自定义属性的程序集中的所有类型? 所以如果我有课 [Findable] public class MyFindableClass {} 我希望能够在Assembly.GetTypes(…)返回的类型集合中找到它。 我可以用一个很大的邪恶黑客做到这一点,但我确信有人有更好的方式。

有没有办法在c#中获取对调用对象的引用?

我想知道的是,是否可以(例如)走向堆栈帧,检查每个调用对象以查看是否匹配接口,如果是,则从中提取一些数据。 是的,我知道这是不好的做法,我想知道是否可能。

使用reflection获取基类的私有属性/方法

使用Type.GetProperties()您可以检索当前类的所有属性以及基类的public属性。 是否有可能获得基类的private属性? 谢谢 class Base { private string Foo { get; set; } } class Sub : Base { private string Bar { get; set; } } Sub s = new Sub(); PropertyInfo[] pinfos = s.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static); foreach (PropertyInfo p in pinfos) { Console.WriteLine(p.Name); } Console.ReadKey(); 这只会打印“Bar”,因为“Foo”属于基类和私有。

C#:通过reflection访问inheritance的私有实例成员

我是C#反思的绝对新手。 我想使用reflection来访问类中的所有私有字段,包括那些inheritance的字段。 我已成功访问除inheritance的所有私有字段,以及所有公共和受保护的inheritance字段。 但是,我无法访问私有的inheritance字段。 以下示例说明: class A { private string a; public string c; protected string d; } class B : A { private string b; } class test { public static void Main(string[] Args) { B b = new B(); Type t; t = b.GetType(); FieldInfo[] fields = t.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); foreach(FieldInfo […]

有人知道快速获取枚举值的自定义属性吗?

这可能是最好的例子。 我有一个属性的枚举: public enum MyEnum { [CustomInfo(“This is a custom attrib”)] None = 0, [CustomInfo(“This is another attrib”)] ValueA, [CustomInfo(“This has an extra flag”, AllowSomething = true)] ValueB, } 我想从实例中获取这些属性: public CustomInfoAttribute GetInfo( MyEnum enumInput ) { Type typeOfEnum = enumInput.GetType(); //this will be typeof( MyEnum ) //here is the problem, GetField takes a string […]

如何确定任意一块T-SQL所需的参数?

基本上,我正在寻找一个适用于任意T-SQL的SqlCommandBuilder.DeriveParameters 。 例如,此查询需要一个参数: SELECT @Foo [Foo], ‘@Bar’ [Bar], @Baz [Baz] 我基本上需要提取: new[] { “Foo”, “Baz” } 从上面。 我可以构建一个SQL解析器,但是我有一个到SQL服务器的开放连接,所以如果可能的话我更愿意使用现有的选项。 编辑: 必须有一种方法可以做到这一点,因为SQL Server的商业智能开发工作室能够非常成功地完成这项工作。 编辑2: SQL BIDS正在执行此命令以描述结果: exec sp_executesql N’SET FMTONLY OFF;SET FMTONLY ON;SELECT @Foo [Foo], ”@Bar” [Bar], @Baz [Baz]’, N’@Foo sql_variant,@Baz sql_variant’, @Foo=NULL,@Baz=NULL 这解释了它如何确定列,但它可能只是字符串解析来获取参数…

使用c#中Type.GetType()返回的类型

我有一个问题,如何使用Type.GetType()返回的类型引用(如果可能的话),例如,创建该类型的IList? 这是示例代码: Type customer = Type.GetType(“myapp.Customer”); IList customerList = new List(); // got an error here =[ 先感谢您 !