Tag: reflection

如何在运行时在IEnumerable 上创建动态选择?

鉴于我有一个IEnumerable ,其中T是任何对象,我如何从中选择一个特定的属性,因为我知道运行时其中一个属性名称的名称是一个字符串? 例如: var externalIEnumerable = DataPassedFromConsumingCode(); // `IEnumerable` string knownPropertyName = “Foo”; var fooSelect = externalIEnumerable.Select(…); 本质上,我显然只是做了externalIEnumerable.Select(x=> x.Foo); ,但我需要在运行时执行此Select ,当我无法控制它最初创建时。 – 答案:根据AlanT的回答,这是我实际做的: public Expression<Func> SelectExpression(string fieldName) { var param = Expression.Parameter(typeof(TItem), “item”); var field = Expression.Property(param, fieldName); return Expression.Lambda<Func>(field, new ParameterExpression[] { param }); } 我把它保存为Expression,因为调用Compile导致IQueryable被枚举,这意味着数据库被不必要地命中。 因此,要使用它,我只需执行以下操作: string primaryKey = _map.GetPrimaryKeys(typeof(TOriginator)).Single(); var primaryKeyExpression = […]

C#中’is’reflection的性能特征是什么?

它表明 ‘as’铸造比前缀铸造快得多,但是’是’reflection怎么样? 有多糟糕? 您可以想象,在Google上搜索“is”并不是非常有效。

是否可以通过reflection调用值类型操作符?

因为C#运算符例如+,+ =,==是可覆盖的。 它让我认为它们是一种方法,因此想知道是否有一种方法可以在Int32上使用reflection来调用它们。

如何枚举传递的方法参数

可以枚举被调用的方法参数类型/信息,如下所示: private void SomeMethod(int thisValue, string thatValue) { StackTrace stackTrace = new StackTrace(); foreach (ParameterInfo pInfo in stackTrace.GetFrame(0).GetMethod().GetParameters()) { string name = pInfo.Name; string type = pInfo.GetType().ToString(); } } 但有没有办法获得每个参数的实际对象? 编辑 :我的目标是枚举所有参数并获取它们的值。 使用LinQ表达式,可以获得如下参数值: private void SomeMethod(int thisValue, string thatValue) { object valueOfThis = GetParameterValue(() => thisValue); object valueOfThat = GetParameterValue(() => thatValue); } private object […]

保留动态调用方法的exception

有关 有关 我想动态调用一个MethodInfo对象,并从内部抛出任何exception向外传递,就像它被正常调用一样。 我看来有两种选择。 它们概述如下。 选项1维护MyStaticFunction抛出的exception类型,但StackTrace因throw被破坏。 选项2维护exception的StackTrace ,但exception的类型始终是TargetInvocationException 。 我可以提取InnerException及其类型,但这意味着我不能写这个例子: try { DoDynamicCall(); } catch (MySpecialException e) { /* special handling */ } 选项1: void DoDynamicCall() { MethodInfo method = /*referencing MyClass method void MyStaticFunction(int x)*/; try { method.Invoke(null, new object[] { 5 }); } catch (TargetInvocationException e) { throw e.InnerException; } } 选项2: void […]

如何创建和访问在C#中作为参数传递的匿名类的新实例?

我创建了一个函数,它接受一个SQL命令并生成输出,然后可以用它来填充类实例的List。 代码效果很好。 我在这里包含了一个稍微简化的版本,没有exception处理仅供参考 – 如果你想直接解决问题,请跳过此代码。 如果你在这里有建议,我会全力以赴。 public List ReturnList() where T : new() { List fdList = new List(); myCommand.CommandText = QueryString; SqlDataReader nwReader = myCommand.ExecuteReader(); Type objectType = typeof (T); FieldInfo[] typeFields = objectType.GetFields(); while (nwReader.Read()) { T obj = new T(); foreach (FieldInfo info in typeFields) { for (int i = 0; i […]

使用Reflection.Emit创建实现接口的类

我需要使用Reflection.Emit生成一个实现以下接口的类。 public interface IObject { T Get(string propertyName); } 有没有人有一个例子,我将如何发出以下作为一个简单的测试用例? class GeneratedObject : IObject { public T Get(string propertyName) { // this is the simplest possible implementation return default(T); } }

如何使用C#Reflection将Vaues设置为嵌套属性。?

我试图使用reflection动态地将值设置为类的嵌套属性。 任何人都可以帮我这样做。 我有一个类Region如下所示。 public class Region { public int id; public string name; public Country CountryInfo; } public class Country { public int id; public string name; } 我有一个Oracle Data reader来提供Ref游标的值。 这会给我一个 ID,姓名,COUNTRY_ID,COUNTRY_NAME 我可以通过下面的方式将值分配给Region.Id,Region.Name。 FieldName=”id” prop = objItem.GetType().GetProperty(FieldName, BindingFlags.Public | BindingFlags.Instance); prop.SetValue(objItem, Utility.ToLong(reader_new[ResultName]), null); 对于嵌套属性,我可以通过读取Fieldname创建实例来为下面的值赋值。 FieldName=”CountryInfo.id” if (FieldName.Contains(‘.’)) { Object NestedObject = objItem.GetType().GetProperty(Utility.Trim(FieldName.Split(‘.’)[0]), BindingFlags.Public | […]

IQueryable OfType 其中T是运行时类型

我需要能够得到类似以下的东西才能工作: Type type = ??? // something decided at runtime with .GetType or typeof; object[] entityList = context.Resources.OfType().ToList(); 这可能吗? 如果有任何新内容允许,我可以使用.NET 4。

Reflection.Emit比GetValue和SetValue更好:S

我被告知使用Reflection.Emit而不是PropertyInfo.GetValue / SetValue,因为这种方式更快。 但我真的不知道Reflection.Emit中有什么东西以及如何用它代替GetValue和SetValue。 任何人都可以帮我吗?