Tag: reflection

克隆/复制将访问者主体添加到新类型

我正在从现有类型创建动态程序集中的新类型,但只包含选定的属性: public class EmitTest { public Type Create(Type prototype, Type dynamicBaseType, List includedPropertyList) { AssemblyName aName = new AssemblyName(“DynamicAssembly”); AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly( aName, AssemblyBuilderAccess.RunAndSave); ModuleBuilder modulBuilder = assemblyBuilder.DefineDynamicModule(aName.Name, aName.Name + “.dll”); string typeName = string.Concat(prototype.Name, “_DynamicType_”, Guid.NewGuid().ToString().Replace(“-“, string.Empty)); TypeBuilder typeBuilder = modulBuilder.DefineType( typeName, TypeAttributes.Public, null, new Type[] { }); foreach (string s in includedPropertyList) […]

使用属性的名称构建OrderBy表达式

我试图通过MVC3中的WebGrid控件来支持排序,MVC3通过sort参数将模型上的属性名称传递给我的操作。 public class Agent { public int Id { get; set; } public string Name { get; set; } } [HttpGet] public ActionResult Index(string sort = “Id”, string sortdir = “ASC”) { // Define the parameter that we are going to use in the OrderBy clause. var param = Expression.Parameter(typeof(Agent), “agent”); // Now we’ll make […]

动态可交换数据访问层

我正在编写一个数据驱动的WPF客户端。 客户端通常从WCF服务中提取数据,该服务查询SQL数据库,但我希望选择直接从SQL或其他任意数据源提取数据。 我想出了这个设计,并希望听听你对它是否是最好的设计的看法。 首先,我们要从SQL中提取一些数据对象。 // The Data Object with a single property public class Customer { private string m_Name = string.Empty; public string Name { get { return m_Name; } set { m_Name = value;} } } 然后我计划使用所有数据访问层应该实现的接口。 假设一个人也可以使用抽象类。 思考? // The interface with a single method interface ICustomerFacade { List GetAll(); } 可以创建SQL实现。 // […]

如何检索.NET运行时生成的所有已关闭generics类型的列表?

根据MSDN文档,.NET运行时将根据需要动态生成基于generics类型定义的封闭类型。 https://msdn.microsoft.com/en-us/library/f4a6ta2h.aspx 是否可以检索与运行时生成的闭合类型相对应的System.Type实例的集合?

C#Reflection SetValue()找不到set accessor

我使用reflection来更新已对其进行更新并保存到mongodb的对象 private void updateSelf(MongoDoc newDoc) { Type type = this.GetType(); foreach (var i in type.GetProperties()) { if (i.GetCustomAttributes(false).Any(x => x is MongoDB.Bson.Serialization.Attributes.BsonIgnoreAttribute)) continue; Object oldValue = i.GetValue(this, null); Object newValue = i.GetValue(newDoc, null); if (!Object.Equals(oldValue, newValue) && !((oldValue == null) && (newValue == null))) { i.SetValue(this, newValue, null); } } } 这大部分工作,但i.SetValue(this, newValue, null); 尝试更新此属性时抛出exception: […]

如何从Lambda表达式的String动态创建方法

我的最终目标是创建一个函数,将函数方法名称动态传递给Hangfire库中的类。 例如,以下是非动态代码: RecurringJob.AddOrUpdate(() => myFunction(), Cron.Hourly) AddOrUpdate的第一个参数的类型是Expression 。 我的第一步是使用reflection来动态插入函数名称: Type thisControllerType = this.GetType(); MethodInfo method = thisControllerType.GetMethod(methodName); //methodName passed as string parameter RecurringJob.AddOrUpdate(() => method.Invoke(this, null), Cron.Hourly); 当我检查Hangfire仪表板时,似乎该表达式被评估为MethodBase.Invoke 。 所以我需要帮助动态传递方法名称。 这可能足以回答我的问题,但我采取的另一条路径是尝试为参数生成整个表达式。 RecurringJob.AddOrUpdate(CreateCallExpression(method), Cron.Hourly); public Expression CreateCallExpression(MethodInfo method) { //trying to pass in zero argument parameter, not sure if this syntax is correct var parameter = […]

在运行时合并两个程序集 – C#

是否可以在运行时合并两个程序集,以便在迭代ExportedTypes时,返回两个原始程序集中的所有类型? 原因: 我使用数据库迁移框架来更新我的数据库。 它需要一个由特殊类组成的程序集,它定义了对数据库所做的修改。 我有两个非常相似的数据库。 许多表都是相同的,但每个表都有几个独特的表。 我想为每个数据库提供一般迁移程序集和特定程序集。 但是要将它与我的框架一起使用,我需要将两个程序集填充到一个程序集中。 这可能吗?

reflection:在运行时将事件字段与委托类型字段区分开来

我的主要问题是:在reflection中是否有可能将某个委托类型的字段与事件用作存储字段的字段区分开来? 这归结为一个问题:FieldInfo类是否包含有关它是否属于某个事件的信息,如存储字段? 我找不到任何可能告诉的属性,也没有找到属性属性。 在下面的代码中,SomeField和SomeEvent的FieldInfos的相关属性是相同的。 因此我不知道如何根据FieldInfos是否为eventstoragefield来对其进行排序。 using System; using System.Reflection; using System.Runtime.CompilerServices; namespace Test { class Program { public Action SomeField; public event Action SomeEvent; static void Main(string[] args) { FieldInfo[] fields = typeof(Program).GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); foreach (FieldInfo fi in fields) Console.WriteLine(string.Format(“FieldName: {0}, Accessibility: {1}, Has Attributes: {2}.”, fi.Name, fi.Attributes, fi.GetCustomAttributes(true).Length != 0)); Console.ReadLine(); […]

具有generics返回类型但不是通用输入的方法。 这可能吗?

假设我们有一个NodeData类: public class NodeData { public string Name; public T Value; public NodeData(string name, T value) { this.Name = name; this.Value = value; } } 以及具有NodaData类型的多个属性的基类Node类和子类: public class Node { public List<NodeData> listOutputs() { var fieldInfos = GetType().GetFields(); var list = new List<NodeData>(); foreach (var item in fieldInfos) { Type t = item.FieldType; string name […]

将C#reflection代码移植到Metro-Ui

我正在尝试移植使用reflection的现有C#类(通用工厂),但我无法编译这段代码: Type[] types = Assembly.GetAssembly(typeof(TProduct)).GetTypes(); foreach (Type type in types) { if (!typeof(TProduct).IsAssignableFrom(type) || type == typeof(TProduct)) … 我试着查看.NET Framework for Windows Metro Style Apps和Assembly Class中的Reflection ,在那里我找到了一个因为“使用System.Security.Permissions”而无法编译的示例。