Tag: fieldinfo

获取字段的行号而不使用ac#parser

我想获得一个类型字段的行#s。 要获取方法中语句的行#,这很简单: Type type = typeof(MyClass); MethodInfo methodInfo = type.GetMethod(“SomeMethod”); int token = methodInfo.MetadataToken; ISymbolReader reader = SymUtil.GetSymbolReaderForFile(@”dllName”, null); // from mike stall’s pdb2xml ISymbolMethod methodSymbol = reader.GetMethod(new SymbolToken(token)); int count = methodSymbol.SequencePointCount; ISymbolDocument[] docs = new ISymbolDocument[count]; int[] startColumn = new int[count]; int[] endColumn = new int[count]; int[] startRow = new int[count]; int[] endRow […]

C#使用reflection开发.Net3.5来获取/设置嵌套属性和/或嵌套字段的值

我正在开发一个适用于从基类inheritance的数据块类的应用程序,我正在尝试使用Reflection深入到我的数据块类中的属性/字段。 由于所有数据块类都是从基类派生/inheritance的(包含Size属性),我可以使用类型基类的通用变量来轻松地在我的应用程序中创建一个对象; 我也可以在顶层获得/设置属性。 我的问题发生在属性是一个字段时 – 我不知道如何进入字段中的下一级别以获取基本属性和/或字段(如果适用)。 我的基础类: namespace MyBase { public class BaseClass { private int _size; public BaseClass() { } public BaseClass(int size) { _size = size; } public int Size() { return _size; } } } 数据块类#1: namespace DataBlock_class { //Data block class #1: (contains simple properties – will be used later) public […]

在C#中使用reflection获取字段的属性

我写了一个从对象中提取字段的方法,如下所示: private static string GetHTMLStatic(ref Object objectX, ref List ExludeFields) { Type objectType = objectX.GetType(); FieldInfo[] fieldInfo = objectType.GetFields(); foreach (FieldInfo field in fieldInfo) { if(!ExludeFields.Contains(field.Name)) { DisplayOutput += GetHTMLAttributes(field); } } return DisplayOutput; } 我的类中的每个字段也都有自己的属性,在这种情况下,我的属性称为HTMLAttributes。 在foreach循环中,我试图获取每个字段的属性及其各自的值。 它目前看起来像这样: private static string GetHTMLAttributes(FieldInfo field) { string AttributeOutput = string.Empty; HTMLAttributes[] htmlAttributes = field.GetCustomAttributes(typeof(HTMLAttributes), false); foreach (HTMLAttributes […]

有没有办法创建一个委托来获取和设置FieldInfo的值?

对于属性,有GetGetMethod和GetSetMethod以便我可以这样做: Getter = (Func)Delegate.CreateDelegate(typeof(Func), propertyInfo.GetGetMethod()); 和 Setter = (Action)Delegate.CreateDelegate(typeof(Action), propertyInfo.GetSetMethod()); 但是我该如何处理FieldInfo ? 我不是在寻找GetValue和SetValue代表(这意味着我每次都会调用reflection) Getter = s => (T)fieldInfo.GetValue(s); Setter = (s, t) => (T)fieldInfo.SetValue(s, t); 但是如果这里有CreateDelegate方法吗? 我的意思是, 因为赋值返回一个值 ,我可以将赋值视为一种方法吗? 如果有的话,它有一个MethodInfo句柄吗? 换句话说,我如何传递正确的MethodInfo设置并从成员字段获取值到CreateDelegate方法,以便我得到一个委托,我可以直接读取和写入字段? Getter = (Func)Delegate.CreateDelegate(typeof(Func), fieldInfo.??); Setter = (Action)Delegate.CreateDelegate(typeof(Action), fieldInfo.??); 我可以构建表达式并编译它,但我正在寻找更简单的东西。 最后,如果问题没有答案,我不介意去表达路线 ,如下所示: var instExp = Expression.Parameter(typeof(S)); var fieldExp = Expression.Field(instExp, fieldInfo); Getter = Expression.Lambda<Func>(fieldExp, instExp).Compile(); […]