Tag: reflection

如何从StackTrace中获取参数值

在方法调用中,我需要在堆栈中“跳转”三层并检索传递给该方法的参数的类型和值。 获取参数类型很简单,但我找不到将值传递给堆栈上的某个方法的方法。 var st = new StackTrace(); var frames = st.GetFrames(); var methodParameters = frame[2].GetMethod().GetParameters; // get each parameter value 注意:使用StackTrace不是必需的。 有没有办法在运行时找到传递给方法的参数值?

如何确定ParameterInfo是否为generics类型?

我有一个GenericMethodDefinition的MethodInfo 。 如: CallMethod(T arg, string arg2) 。 GetParameters()方法将为我提供两个ParameterInfo对象,第一个是通用的,第二个不是。 如何让ParameterInfo告诉我它是通用的? 如果有约束怎么办?

通过使用不同属性类型的reflection设置对象的属性

我使用reflection来填充对象的属性。 这些属性有不同的类型:String,Nullable(double)和Nullable(long)(不知道如何在这里转义尖括号……)。 这些属性的值来自(字符串,对象)对的字典。 因此,例如我的类具有以下属性: string Description { get; set; } Nullable Id { get; set; } Nullable MaxPower { get; set; } (实际上有大约十几个属性),字典将有,,等条目 现在我使用类似以下内容来设置值: foreach (PropertyInfo info in this.GetType().GetProperties()) { if (info.CanRead) { object thisPropertyValue = dictionary[info.Name]; if (thisPropertyValue != null && info.CanWrite) { Type propertyType = info.PropertyType; if (propertyType == typeof(String)) { info.SetValue(this, Convert.ToString(thisPropertyValue), […]

WP7.1上的匿名类型和Get访问器?

我正在尝试将一个简单的对象写入Dictionary转换器,如下所示: public static class SimplePropertyDictionaryExtensionMethods { public static IDictionary ToSimplePropertyDictionary(this object input) { if (input == null) return new Dictionary(); var propertyInfos = from property in input.GetType() .GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy | BindingFlags.GetProperty) where property.CanRead select property; return propertyInfos.ToDictionary(x => x.Name, x => input.GetPropertyValueAsString(x)); } public static string GetPropertyValueAsString(this object input, PropertyInfo propertyInfo) { […]

过滤掉Type.GetMethods()返回的自动生成的方法(getter / setter / add / remove / .etc)

我使用Type.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)来检索给定类型的方法数组。 问题是返回的MethodInfo可能包含由我不想要的编译器生成的方法。 例如: property bool Enabled { get; } 将得到bool get_Enabled() 事件SomethingChanged将获得add_SomethingChanged(EventHandler)和remove_SomethingChanged(EventHandler) 我可以添加一些filter逻辑来摆脱它们,这可能会变得非常复杂。 我想知道是否还有其他我可以做的事情,例如使用BindingFlags设置,只检索用户定义的方法?

如何用reflection调用List 的扩展方法“ElementAt”?

我有问题,在创建List 类型的对象“oListType01”之后,在将其分配给另一个类型为“object”的objet“oObjectType”后,我无法访问任何更多的函数“ElementAt(1)”。 我尝试使用reflection,但我总是在“调用”方法中获得exception(参数冲突)。 有谁知道为什么? 米兰 MyClass01 oMy1 = new MyClass01(); oMy1._ID = “1”; MyClass01 oMy2 = new MyClass01(); oMy2._ID = “3”; IList oListType01 = new List(); oListType01.Add(oMy1); oListType01.Add(oMy2); object oObjectType = new object(); oObjectType = oListType01; 从这里fowrads只有对象oObjectType可用(在实际情况下向上发生在单独的函数调用中)。 在VS oObjectType中显示了我想要反映的两个元素。 MethodInfo mInfo = typeof(System.Linq.Enumerable).GetMethod(“ElementAt”).MakeGenericMethod(typeof(object)); object oSingleObject = mInfo.Invoke(oObjectType, new object[] { 1 });

如何正确反映基接口方法

我通过Reflection调查了2个接口和2个类: IParent IChild – 源自IParent 亲 孩子 – 来自父母 对我来说奇怪的是,当我通过对IChild类型的反思时,我找不到IParent方法。 应用于Child类型的相同代码按预期工作 – reflection显示Parent方法。 interface IParent { void ParentMethod(); } interface IChild : IParent { void ChildMethod(); } class Parent { public void ParentMethod(){} } class Child : Parent { public void ChildMethod(){} } void Main() { //investigate derived interface Type t = typeof(IChild); var info […]

如果在运行时只知道类型参数,如何调用generics方法?

我有这个方法: public List SomeMethod( params ) where T : new() 所以我想把这个SomeMethod ,如果我知道这个类型就好了: SomeMethod(); 但如果我在运行时只有Class1 ,我就无法调用它? 那么如何用未知的T类型调用SomeMethod ? 我用reflection得到了Type。 我有Type类型,但SomeMethod SomeMethod不起作用。 更新7. May: 以下是我想要实现的示例代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; namespace ConsoleApplication63 { public class DummyClass { } public class Class1 { public string Name; } class AssemblyTypesReflection { static void Main(string[] […]

使用reflection在类实例中按名称获取属性的值

可以说我有 class Person { public Person(int age, string name) { Age = age; Name = name; } public int Age{get;set} public string Name{get;set} } 我想创建一个接受包含“age”或“name”的字符串的方法,并返回一个具有该属性值的对象。 像下面的伪代码: public object GetVal(string propName) { return .value; } 我怎么能用reflection做到这一点? 我使用asp.net 3.5编译,c#3.5

如何通过Reflection获取属性的DisplayAttribute?

我有一个像这样的Helper方法来获取PropertyName(试图避免魔术字符串) public static string GetPropertyName(Expression<Func> expression) { var body = (MemberExpression) expression.Body; return body.Member.Name; } 但有时我的PropertyNames也没有好好命名。 所以我想宁愿使用DisplayAttribute。 [Display(Name = “Last Name”)] public string Lastname {get; set;} 请注意我使用的是Silverlight 4.0。 我无法找到通常的命名空间DisplayAttributeName属性。 如何更改我的方法来读取eproperty的属性(如果可用)? 非常感谢,