Tag: reflection

C#:如何获取types的所有公共(包括get和set)string属性

我试图做一个方法,将通过通用对象的列表,并replace所有他们的属性typesstring是null或空replace。 如何做到这一点的好方法? 我有这种…壳…迄今: public static void ReplaceEmptyStrings<T>(List<T> list, string replacement) { var properties = typeof(T).GetProperties( — What BindingFlags? — ); foreach(var p in properties) { foreach(var item in list) { if(string.IsNullOrEmpty((string) p.GetValue(item, null))) p.SetValue(item, replacement, null); } } } 那么,我如何find一个types的所有属性: string的types 公众get 有公共set ? 我做了这个testing课: class TestSubject { public string Public; private string Private; public […]

如何在C#中创build一个简单的dynamic代理

我想要构build一个dynamic的代理对象来为某个对象添加某些function。 基本上我想接收一个对象,用一个看起来与我原来相同的对象包装它,并拦截所有的调用。 class Wrapper : DynamicProxy// dynamic proxy is not a reall class, but i guess something like this exists… { public static T Wrap(T obj) { return (T) new Wrapper(obj); } public override object InterceptCall(MethodInfo info, object[] args) { // do stuff } } 只是为了澄清,我想做一些类似于WCF频道工厂… 我添加一个赏金,因为我需要一个很好的方法来代理类(而不是接口),并处理非虚拟方法(就像我inheritance和添加一个方法在“新”关键字下)。 我确信这一切都是非常可能的。

如何将string转换为任何types

我想将一个string转换为一个genericstypes 我有这个: string inputValue = myTxtBox.Text; PropertyInfo propInfo = typeof(MyClass).GetProperty(myPropertyName); Type propType = propInfo.PropertyType; object propValue = ????? 我想将'inputString'转换为该属性的types,以检查它是否兼容我该怎么做? TKS

如何从Enum中获取数值?

例如System.Net.HttpStatusCode枚举,我想获得HTTP状态代码而不是HTTP状态文本System.Net.HttpStatusCode.Forbidden应该返回403而不是“Forbidden” 我如何提取价值?

呼叫和Callvirt

CIL指令“Call”和“Callvirt”之间有什么区别?

.NET – 获取reflection的PropertyInfo的默认值

今天真的很难过 我确定它不那么难,但我有一个System.Reflection.PropertyInfo对象。 我想根据数据库查找的结果设置它的值(想想ORM,将列映射回属性)。 我的问题是,如果数据库返回值是DBNull,我只想将属性值设置为其默认值,就像调用: value = default(T); // where T is the type of the property. 然而,如果你给它一个Type,那么default()方法将不会被编译,这就是我所拥有的: object myObj = ???; // doesn't really matter. some arbitrary class. PropertyInfo myPropInf = ???; // the reflection data for a property on the myObj object. myPropInf.SetValue(myObj, default(myPropInf.PropertyType), null); 以上不编译。 默认(Type)无效。 我也想过这样做: object myObj = ???; PropertyInfo myPropInf […]

在所有组件中查找types

我需要在网站或Windows应用程序的所有程序集中查找特定的types,有没有简单的方法来做到这一点? 就像ASP.NET MVC的控制器工厂如何查看控制器的所有组件一样。 谢谢。

NoSuchFieldException当字段存在

尝试运行以下方法时出现java.lang.NoSuchFieldException : public void getTimes(String specialty, String day) { ArrayList<Tutor> withSpec = new ArrayList<Tutor>(); for (Tutor t : tutorList){ try { Time startTime = (Time)t.getClass().getField(day + "Start").get(t); } catch (NoSuchFieldException | SecurityException | IllegalAccessException ex) Logger.getLogger(DBHandler.class.getName()).log(Level.SEVERE, null, ex); } 错误是在行Time startTime = (Time)t.getClass().getField(day + "Start").get(t); 我不明白这个错误,因为monStart是Tutor类的一个字段: Public class Tutor implements Serializable { private static final […]

java中的stringreplace,类似于速度模板

在Java中是否存在任何Stringreplace机制,我可以在其中使用文本传递对象,并在string出现时replacestring。 例如,文本是: Hello ${user.name}, Welcome to ${site.name}. 我有的对象是"user"和"site" 。 我想用${}给出的stringreplace对象中的等价值。 这与我们replace速度模板中的对象相同。

如何通过reflection获得主动logging关联

对于普通的列,你可以通过columns类的方法得到它们。 但是,如果在关系方法中设置了foreign_key选项,那么关联可能会被命名为完全不同。 例如,给出 class Post has_many :comments, :foreign_key => :message_id # this is a contrived example end 如果我做Post.column_names我可以在message_id ,但有什么办法可以得到comments ?