Tag: reflection

激活器和静态类

我正在抛弃使用Activator类的想法,以便访问程序集中的资源,否则我将为(dependency injection)创建循环引用。 我之前已经完成了我需要引用的vanilla类,但我的问题是:我可以使用Activator来访问静态类吗? 绊倒我的那部分是Activator返回给你一个对象的实例 ,而一个静态类没有实例。 这可能吗?

我可以通过reflection/诊断从方法中获取调用实例吗?

有没有办法通过System.Reflection,System.Diagnostics或其他方法来获取对调用静态方法的实际实例的引用,而不将其传递给方法本身? 例如,沿着这些方向的东西 class A { public void DoSomething() { StaticClass.ExecuteMethod(); } } class B { public void DoSomething() { SomeOtherClass.ExecuteMethod(); } } public class SomeOtherClass { public static void ExecuteMethod() { // Returns an instance of A if called from class A // or an instance of B if called from class B. object caller […]

C#反思:如何获取Nullable 的类型?

我想做的是这样的: switch( myObject.GetType().GetProperty( “id”) ) { case ??: // when Nullable, do this case ??: // when string, do this case ??: // when Nullable, do this object.GetType()下的什么路径将具有我可以使用case语句进行比较的数据类型的字符串名称? 我需要知道类型,所以我可以使用许多Convert.ToInt32(字符串)中的一个,它将使用Reflection设置myObject的值。

“动态加载可移植类库时无法加载文件或程序集’System.Core,Version = 2.0.5.0,…”exception

首先,我需要强调的是,这个问题与这个post中的问题略有不同。 此外,安装KB2468871没有帮助。 我试图尽可能地简化这个问题。 一般来说,它是关于使用Assembly.LoadFile(…)在Desktop应用程序中加载PCL程序集。 假设有一个.NET 4.0控制台应用程序(称为“C”)。 它引用了.NET 4.0程序集(称为“N4”)和PCL程序集(称为“PCL”)。 N4看起来像这样: using System.Linq; namespace N4 { public class ClassInN4 { public static string Greet() { return new string( “hello from N4” .ToCharArray() .Select(char.ToUpper) .ToArray() ); } } } PCL看起来像这样: using System.Linq; namespace PCL { public class ClassInPCL { public static string Greet() { return new string( “hello […]

为Type.GetMethod指定params

我正在使用reflection来获取TryParse方法信息(为第一个人提供upvote以猜测原因;)。 如果我打电话: typeof(Int32).GetMethod(“Parse”, BindingFlags.Static | BindingFlags.Public, null, new Type[] { typeof(string) }, null); 我得到一个方法,但稍微扩展一下: typeof(Int32).GetMethod(“TryParse”, BindingFlags.Static | BindingFlags.Public, null, new Type[] { typeof(string), typeof(Int32) }, null); 我一无所获。 我的spidersense告诉我这是因为第二个参数是out参数。 谁知道我在这里做错了什么?

获取没有任何generics信息的类型名称

如果我写: var type = typeof(List); Console.WriteLine(type.Name); 它会写: List`1 我希望它只写: 名单 我怎样才能做到这一点? 有没有更聪明的方法来实现它而不必使用Substring或类似的字符串操作函数?

如何获取应用程序使用的DLL的名称

我正在寻找阅读我的应用程序使用的所有程序集(.dll)的方法。 在标准的C#项目中有“References”文件夹,当它被展开时,我可以读取所有使用的库。 我的目标是以编程方式读取我的解决方案中每个项目使用的所有程序集。 最后,我想看看我编译的* .exe应用程序使用了哪些库。

如何在使用reflection加载的程序集中使用Castle.Windsor

假设我有一个库Lib.dll,它使用Castle.Windsor初始化其服务。 我有一个主应用程序App.exe,它使用reflection在运行时加载Lib.dll。 App.exe事先并不知道Lib.dll的位置,它只在运行时才知道。 在这种情况下,当App.exe加载Lib.dll和Lib.dll初始化其服务时,会抛出System.TypeInitializationExceptionexception,因为Castle.Windsor无法找到服务类型。 Castle.MicroKernel.SubSystems.Conversion.ConverterException: Could not convert from ‘Lib.TheServiceClass’ to System.Type – Maybe type could not be found at Castle.MicroKernel.SubSystems.Conversion.TypeNameConverter.PerformConversion(String value, Type targetType) in e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\MicroKernel\SubSystems\Conversion\TypeNameConverter.cs:line 91 at Castle.MicroKernel.SubSystems.Conversion.DefaultConversionManager.PerformConversion(String value, Type targetType) in e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\MicroKernel\SubSystems\Conversion\DefaultConversionManager.cs:line 134 at Castle.MicroKernel.SubSystems.Conversion.DefaultConversionManager.PerformConversion[TTarget](String value) in e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\MicroKernel\SubSystems\Conversion\DefaultConversionManager.cs:line 162 at Castle.Windsor.Installer.DefaultComponentInstaller.SetUpComponents(IConfiguration[] configurations, IWindsorContainer container, IConversionManager converter) in e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\Windsor\Installer\DefaultComponentInstaller.cs:line 196 at Castle.Windsor.Installer.DefaultComponentInstaller.SetUp(IWindsorContainer container, IConfigurationStore […]

查找对象的所有属性和子属性

有时我想知道一个对象是否有我正在寻找的属性,但有时一个对象有很多属性,可能需要一些时间才能找到它进行调试。 如果我能编写一个能够在字符串中找到所有属性及其值的函数,那么我可以将该字符串粘贴到记事本中,并查找我正在寻找的值,并使用记事本具有的查找function。 到目前为止,我有这样的事情: public void getAllPropertiesAndSubProperties(System.Reflection.PropertyInfo[] properties) { foreach (var a in properties) { //MessageBox.Show(a.ToString()); // do something here to test if property a is the one // I am looking for System.Reflection.PropertyInfo[] temp = a.GetType().GetProperties(); // if property a has properties then call the function again if (temp.Length > 0) getAllPropertiesAndSubProperties(temp); } } 编辑我工作过的问题: […]

用reflection动态调用textboxfor

我想要做的最终结果是通过反映对象及其属性来动态构建表单。 我已经创建了调用TextBoxFor和CheckBoxFor等的HtmlHelper方法,但是现在我需要帮助找出在将它传递给Html.TextBoxFor时如何正确反映属性 这是辅助方法: public static MvcHtmlString FormTextBox(this HtmlHelper helper, String id, String property_name, object model, RouteValueDictionary attributes) { Type model_type = model.GetType(); return helper.TextBoxFor(model_object => model_type.InvokeMember(property_name, BindingFlags.ExactBinding | BindingFlags.GetProperty, null, model, null)); } 但它在返回时会出现此错误代码: 模板只能用于字段访问,属性访问,单维数组索引或单参数自定义索引器表达式。 基本上我想采取这样的方式: @Html.TextBoxFor(model => model.Name) 把它变成这个: @FormHelpers.FormTextBox(“Name”, model) 并让它输出相同的东西。 UPDATE 我将重申这一点,因为我在解决问题方面取得了一些进展。 我已经从Expression.PropertyOrField创建了一个Expression,它可以创建我正在寻找的东西。 但是我无法让TextBoxFor函数接受它。 Expression fieldExpr = Expression.PropertyOrField(Expression.Constant(model),property_name); return helper.TextBoxFor(Expression.Lambda<Func>(fieldExpr, null).Compile()()); 关于如何正确地将Expression传递给函数的任何想法?