Tag: reflection

为什么我的.NET属性不执行操作?

我创建了一个简单的属性: [AttributeUsage(AttributeTargets.Method)] public class InitAttribute : System.Attribute { public InitAttribute() { Console.WriteLine(“Works!”); } } 我将它应用于一个简单的方法: static class Logger { public static string _severity; public static void Init(string severity) { _severity = severity; } [Init()] public static void p() { Console.WriteLine(_severity); } } 发生了什么是非常好的前进。 只是,我希望该属性执行一个动作(打印Works!),但这不会发生。 上瘾,打印“作品!” 当然只是为了调试目的:我想访问实例的属性_severity (例如,检查是否是!= null),但我一直在阅读的有关属性的内容(对我来说很新)是关于通过reflection访问类的方法或属性等。 一旦我评估了_severity ,我怎样才能修改修饰方法的行为(在这种情况下,引发exception“Logger未初始化”并且不执行它)? 任何帮助赞赏。

ReflectionTypeLoadException:Type正在尝试实现无法访问的接口

我正在使用Assembly.GetTypes()获取插件库中定义的所有类型(因此我可以实例化插件实例)。 在特定的库中,该方法引发ReflectionTypeLoadException,说: Type is attempting to implement an inaccessible interface 谷歌搜索它似乎是因为特定的类型实现了非公共接口。 实际上它是,但Type嵌套在另一个公共类中,声明为private。 如何避免这种exception? …. 制作界面公共代码工作。 是否有可能定义这个奇怪的行为是一个bug(在Assembly.GetTypes()中)? 这意味着库类型无法实现受保护的交互面!

C#反思:如何获取数组值和长度?

FieldInfo[] fields = typeof(MyDictionary).GetFields(); MyDictionary是一个静态类,所有字段都是字符串数组。 如何获取每个数组的Length值,然后遍历所有元素? 我尝试了演员: field as Array 但它会导致错误 无法通过引用转换,装箱转换,拆箱转换,换行转换或空类型转换将类型’System.Reflection.FieldInfo’转换为’System.Array’

使用CompilerParameters引用当前程序集

现在我正在开发一个项目,团队想要一种编写代码并编辑它的方法,而不必重新编译整个项目,所以我决定尝试实现一个脚本引擎。 之前将Lua实现到C ++之后,我并不是一个将脚本function实现到项目中的新手。 但是,我们希望尝试使用Microsoft.CSharp命名空间直接实现C#,并结合已内置于C#的System.Reflection。 所以听说过这个,我在文档中讨论过,我想出了ALMOST工作的原型 – 但并不完全。 using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.CSharp; using System.CodeDom.Compiler; using System.Reflection; namespace Scripting { class Program { static void Main(string[] args) { StringBuilder builder = new StringBuilder(); builder.Append(“using System;”); builder.Append(“using Scripting;”); builder.Append(“class MyScript : IScript”); builder.Append(“{“); builder.Append(” string ScriptName”); builder.Append(” {“); builder.Append(” get { return […]

满足开放/封闭原则的工厂模式?

我有以下具体的Animal产品: Dog和Cat 。 我正在使用参数化的Factory方法来创建所述产品。 根据传递给Factory方法的AnimalInfo参数,将创建具体产品。 映射逻辑放在Factory方法中。 这是我的代码: public abstract class AnimalInfo { public abstract String Sound { get; } } public class DogInfo : AnimalInfo { public override string Sound { get { return “Bark”; } } } public class CatInfo : AnimalInfo { public override string Sound { get { return “Meow”; } } […]

如何使用reflection调用generics类的静态属性?

我有一个类(我无法修改)简化为: public class Foo { public static string MyProperty { get {return “Method: ” + typeof( T ).ToString(); } } } 我想知道当我只有一个System.Type时如何调用这个方法 即 Type myType = typeof( string ); string myProp = ???; Console.WriteLinte( myMethodResult ); 我试过的: 我知道如何使用reflection实例化generics类: Type myGenericClass = typeof(Foo).MakeGenericType( new Type[] { typeof(string) } ); object o = Activator.CreateInstance( myGenericClass ); 但是,由于我使用静态属性,这是否适合实例化一个类? […]

C#使用reflection获取不安全结构中的固定字段类型

我正在尝试使用一些固定字段来获取不安全结构的字段类型。 固定字段FieldType不返回实际类型。 [StructLayout(LayoutKind.Sequential, Pack = 1)] public unsafe struct MyStruct { public UInt32 Field1; public fixed sbyte Field2[10]; public UInt64 Field3; } void Test() { var theStruct = new MyStruct(); string output = “”; foreach (FieldInfo fi in theStruct.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance)) { output += fi.Name + “: ” + fi.FieldType.ToString() + “\r\n”; } } 输出: […]

编译器生成的事件的后备字段是否始终保证使用与事件相同的名称?

C#允许我们创建自定义事件访问器 。 Action _custom; public event Action Custom { add { _custom = (Action)Delegate.Combine( _custom, value ); } remove { _custom = (Action)Delegate.Remove( _custom, value ); } } 如果未指定它们, 编译器将为您创建它们 。 C#语言规范: 编译类似字段的事件时,编译器会自动创建存储以保存委托,并为事件创建访问器,以便向委托字段添加或删除事件处理程序。 反编译的源代码使用dotPeek进行简单的public event Action Public; 看起来如下: private Action Public; public event Action Public { add { Action action = this.Public; Action comparand; do […]

如何在重定向,asp.net mvc3之前确保控制器和操作存在

在我的一个控制器+动作对中,我从另一个地方获取另一个控制器和动作的值作为字符串,我想重定向我当前的动作。 在进行重定向之前,我想确保我的应用程序中存在控制器+操作,如果没有,则重定向到404.我正在寻找一种方法来执行此操作。 public ActionResult MyTestAction() { string controller = getFromSomewhere(); string action = getFromSomewhereToo(); /* At this point use reflection and make sure action and controller exists else redirect to error 404 */ return RedirectToRoute(new { action = action, controller = controller }); } 我所做的就是这个,但它不起作用。 var cont = Assembly.GetExecutingAssembly().GetType(controller); if (cont != null && cont.GetMethod(action) […]

使用reflection来解决Linqed属性

我正在尝试编写一个通用方法,它将加载具有特定ID的特定类型的记录。 这是一种有效的方法: public abstract class LinqedTable where T : LinqableTable { public static T Get(long ID) { DataContext context = LinqUtils.GetDataContext(); var q = from obj in context.GetTable() where obj.ID == ID select obj; return q.Single(); } } public abstract class LinqableTable { public abstract long ID { get; set; } } 您可以忽略对LinqUtils.GetDataContext()的调用; 这是一个实用函数,我必须处理我的程序中有多个数据上下文的事实。 关键是现在我可以将我的任何类声明为LinqableTable子类,并且我可以通过调用LinqedTable.Get(ID)轻松地实例化该表的记录。 […]