Tag: reflection

有没有办法通过C#代码获取您在类中使用的所有命名空间?

有没有办法获得一个List ,其中包含命名空间/类中的所有’usings’? 例如 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Linq.Expressions; using System.Linq.Dynamic; using System.Text.RegularExpressions; using System.Reflection; namespace MyNamespace.Other.Scripting { 我将有一个列表与“系统”,“System.Text”,“System.Reflection”,等等。 编辑:如下面的评论,我正在使用这个http://kamimucode.com/Home.aspx/C-sharp-Eval/1 – C#评估器,我必须为TypeParser提供一个命名空间列表。 问题是,我不会总是知道要放哪些。 我正在使用C#中的一种简单脚本语言,从中我将能够调用C#代码。 我的另一种选择是在脚本语言中创建一个关键字,例如“using”,但我真的不想这样做。 我希望能够选择一个C#对象,通过我的script.txt做任何我想做的事情,并且可以自由地使用它引用的命名空间。 我想在我的Xna Game引擎中使用它,以便能够编写可以执行C#的自定义脚本以及我的简单脚本函数。

反思真的很慢,我不应该在它有意义的时候使用它吗?

可能重复: .NETreflection的代价是多少? 我遇到的问题的“优雅”解决方案是使用属性将类及其属性与另一个类相关联。 问题是,要将它转换为另一个,我必须使用reflection。 我正在考虑将其托管在云端的服务器端应用程序。 我听过很多关于“reflection很慢,不使用它”的隆隆声,慢得多慢? 它是如此占用CPU密集度以至于它会大大增加我的CPU时间,以至于我真的要为我决定在云端架构底部使用reflection付出代价吗?

设置私有字段的值

为什么以下代码不起作用: class Program { static void Main ( string[ ] args ) { SomeClass s = new SomeClass( ); s.GetType( ).GetField( “id” , System.Reflection.BindingFlags.NonPublic ) // sorry reasently updated to GetField from GetProperty… .SetValue( s , “new value” ); } } class SomeClass { object id; public object Id { get { return id; } […]

我可以从Func 获取特定的元数据吗?

请考虑以下代码: string propertyName; var dateList = new List() { DateTime.Now }; propertyName = dateList.GetPropertyName(dateTimeObject => dateTimeObject.Hour); // I want the propertyName variable to now contain the string “Hour” 这是扩展方法: public static string GetPropertyName(this IList list, Func func) { //TODO: would like to dynamically determine which // property is being used in the func function/lambda } […]

MethodInfo声明类型的等式

我需要检查两个MethodInfos之间的相等性。 它们实际上是完全相同的MethodInfo,但ReflectedType除外(也就是说,DeclaringType是相同的,并且方法实际上应该具有相同的主体)。 有很多方法可以做到这一点,但我正在寻找最有效的方法。 现在我有: public static bool AreMethodsEqualForDeclaringType(this MethodInfo first, MethodInfo second) { first = first.ReflectedType == first.DeclaringType ? first : first.DeclaringType.GetMethod(first.Name, first.GetParameters().Select(p => p.ParameterType).ToArray()); second = second.ReflectedType == second.DeclaringType ? second : second.DeclaringType.GetMethod(second.Name, second.GetParameters().Select(p => p.ParameterType).ToArray()); return first == second; } 这有点贵,所以我想知道是否有更好的方法…… 我应该比较两个方法体吗? 例如。 first.GetMethodBody() == second.GetMethodBody() 谢谢。

这个reflection代码出了什么问题? GetFields()返回一个空数组

C#,Net 2.0 这是代码(我拿出了所有特定于域的东西,它仍然返回一个空数组): using System; using System.Collections.Generic; using System.Text; using System.Reflection; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { ChildClass cc = new ChildClass(); cc.OtherProperty = 1; FieldInfo[] fi = cc.GetType().GetFields(); Console.WriteLine(fi.Length); Console.ReadLine(); } } class BaseClass { private int myVar; public int MyProperty { get { return myVar; } set { […]

在C#.NET中即时转换为Type

我想将Object转换为将在运行时分配的类型。 例如,假设我有一个函数将字符串值(从TextBox or Dropdownlist )分配给Object.Property 。 如何将值转换为正确的类型? 例如,它可以是整数,字符串或枚举。 Public void Foo(object obj,string propertyName,object value) { //Getting type of the property og object. Type t= obj.GetType().GetProperty(propName).PropertyType; //Now Setting the property to the value . //But it raise an error,because sometimes type is int and value is “2” //or type is enum (ea: Gender.Male) and value is […]

使用reflection来调用ASP.NET Web服务

假设我有一个ASMX Web服务MyService。 该服务有一个方法,MyMethod。 我可以在服务器端执行MyMethod,如下所示: MyService service = new MyService(); service.MyMethod(); 我需要做类似的事情,服务和方法直到运行时才知道。 我假设反思是解决这个问题的方法。 不幸的是,我很难让它发挥作用。 当我执行此代码时: Type.GetType(“MyService”, true); 它抛出此错误: 无法从程序集“App_Web__ktsp_r0,Version = 0.0.0.0,Culture = neutral,PublicKeyToken = null”加载类型“MyService”。 任何指导将不胜感激。

缓存reflection属性getter / setter的最佳方法是什么?

我知道反思可能很昂贵。 我有一个经常获取/设置属性的类,我想到的一种方法是以某种方式缓存reflection。 我不确定我是否应该缓存表达式或者在这里做什么。 这就是我目前正在做的事情: typeof(T).GetProperty(propName).SetValue(obj, value, null); typeof(T).GetProperty(propName).GetValue(obj, null); 那么……最快的方法是什么?

用于动态加载程序集的App配置

我正在尝试动态地将模块加载到我的应用程序中,但我想为每个模块指定单独的app.config文件。 假设我有以下主应用程序的app.config设置: 另一个用于使用Assembly.LoadFrom加载的库: 两个库都有一个实现相同接口的类,具有以下方法: public string Name { get { return ConfigurationManager.AppSettings[“House”]; } } 肯定从主类和加载的汇编类中调用Name输出Stark 。 有没有办法让主应用程序使用自己的app.config和每个加载的程序集使用他们的? 配置文件的名称在输出中是不同的,所以我认为这应该是可能的。