Tag: reflection

PropertyInfo.SetValue对象与目标类型不匹配

我有一个类似的错误,但不幸的是不是同一个简单的解决方案。 这是代码: public virtual void MapObject(T obj, IViewModel viewModel, ITPSDataAccess dataAccess) { var objProps = obj.GetType().GetProperties(); var dtoProps = viewModel.GetType().GetProperties(); foreach (var dtoProp in dtoProps) { var objProp = dtoProps.SingleOrDefault(x => x.Name == dtoProp.Name); if (objProp != null) { var dtoVal = dtoProp.GetValue(viewModel, null); objProp.SetValue(obj, dtoVal, null); // ERROR HERE } } … } 错误发生在指示的位置,表示“对象与目标类型不匹配”。 […]

如何通过reflection获取透明代理的属性值?

我的代码接收透明代理而不是原始实例。 虽然这个var type = obj.GetType(); 产生原始类的类型,以下代码抛出TargetException : 对象与目标类型不匹配 var value = property.GetValue(obj, null); 其中property是type.GetProperties()

MemberWiseClone如何使用克隆属性创建新对象?

我想创建一个更受限制的MemberwiseClone版本,但是我意识到我自己的C#代码可以为对象添加属性的唯一方法是使用dynamic ,但这不能给对象提供与原始对象相同的类型。 另一个更丑陋的选择是为新克隆发出源代码并在运行时编译它,但这会带来复杂性。 assembly参考等,我不关心保持简单。 现在我只是使用MemberwiseClone ,但我真的很好奇它是如何工作的。 我找不到任何反编译的来源。

reflection发射:如何为此构建构造函数

我想动态构建的代码如下: public class Sample { public Sample() { Items = new ObservableTestCollection(this); } public Sample(IEnumerable source) { Items = new ObservableTestCollection(this, source); } public ObservableTestCollection Items; } ObservableTestCollection的来源如下: public class ObservableTestCollection : ObservableCollection { public T Parent; public ObservableTestCollection(T parent) { Parent = parent; } public ObservableTestCollection(T parent, IEnumerable source) : base(source) { Parent = […]

在使用reflection时,如何确定方法是否返回动态类型?

使用reflection时,可以检查DynamicAttribute属性的字段,属性,索引器和参数,以确定它们具有动态类型。 但是,这对于Methods不起作用 – 即使它们返回’dynamic’,它们也没有属性,它们的返回类型是’object’(也没有属性)。 Visual Studio为intellisense为外部程序集中的方法执行此操作…可以使用reflection来完成吗? 例如,下面的代码生成此输出: dynamicField is dynamic DynamicProperty is dynamic Item is dynamic DynamicMethod is NOT dynamic!! dynamicParameter is dynamic 示例代码: using System; using System.Reflection; using System.Runtime.CompilerServices; namespace ReflectDynamics { class Program { static void Main() { var test = typeof(Test); CheckDynamic(test.GetField(“dynamicField”)); CheckDynamic(test.GetProperty(“DynamicProperty”)); CheckDynamic(test.GetProperty(“Item”)); MethodInfo dynamicMethod = test.GetMethod(“DynamicMethod”); CheckDynamic(dynamicMethod); CheckDynamic(dynamicMethod.GetParameters()[0]); Console.ReadKey(); } […]

C#注册嵌入式Directshowfilter

我正在研究在运行时注册directshowfilter,可能需要使用reflection来执行此操作,然后以某种方式在二进制数据上调用regsvr32。 不确定这是否可行,听起来很棘手。 基本上我有一个dll文件是一个filter,我把它作为一个嵌入式资源添加到解决方案但在此之后我被卡住…不知道如何去注册它。 有人有任何见解吗? 这是可能的,还是我必须要有文件才能注册? 谢谢。 干杯。

ServiceContractGenerator CodeDomProvider编译错误

我正在尝试使用ServiceCodeGenerator和CodeDomProvider来动态创建服务引用。 使用CodeDomProvider编译代码时,会抛出以下错误。 它看起来只适用于特定的Web服务。 我能够编译其他Web服务,但是这个会抛出下面的编译错误。 知道如何编辑源代码或忽略错误吗? CS0579:复制’System.CodeDom.Compiler.GeneratedCodeAttribute’属性99 CS0579:复制’System.Diagnostics.DebuggerStepThroughAttribute’属性101 CS0579:复制’System.CodeDom.Compiler.GeneratedCodeAttribute’属性191 CS0579:复制’System.Diagnostics.DebuggerStepThroughAttribute’属性193 代码如下: Uri address = new Uri(url + “?wsdl”); MetadataExchangeClientMode mexMode = MetadataExchangeClientMode.HttpGet; MetadataExchangeClient metadataExchangeClient = new MetadataExchangeClient(address, mexMode); metadataExchangeClient.ResolveMetadataReferences = true; //Trust all certificates System.Net.ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true); ICredentials networkCredential = new NetworkCredential(“username”, “password”, “domain”); metadataExchangeClient.HttpCredentials = networkCredential; MetadataSet metadataSet = metadataExchangeClient.GetMetadata(); […]

使用带有Delegate.CreateDelegate的值类型的C#

使用Jon Skeet的文章使reflection飞行和探索代表作为指导,我试图使用Delegate.CreateDelegate方法将属性复制为委托。 这是一个示例类: public class PropertyGetter { public int Prop1 {get;set;} public string Prop2 {get;set;} public object GetPropValue(string propertyName) { var property = GetType().GetProperty(propertyName).GetGetMethod(); propertyDelegate = (Func)Delegate.CreateDelegate(typeof(Func), this, property); return propertyDelegate(); } } 我遇到的问题是当我调用GetPropValue并传入”Prop1″作为参数时,我在调用Delegate.CreateDelegate时收到ArgumentException ,并显示消息”Cannot bind to the target method because its signature or security transparency is not compatible with that of the delegate type.” […]

如何使用reflection来调用方法并在编译时传递类型未知的参数?

我想用一个从字符串输入“解析”的参数值动态调用类的方法 。 例如:我想用以下命令调用以下程序: c:> myprog.exe MethodA System.Int32 777 c:> myprog.exe MethodA System.float 23.17 c:> myprog.exe MethodB System.Int32&777 c:> myprog.exe MethodC System.Int32 777 System.String ThisCanBeDone static void Main(string[] args) { ClassA aa = new ClassA(); System.Type[] types = new Type[args.Length / 2]; object[] ParamArray = new object[types.Length]; for (int i=0; i < types.Length; i++) { types[i] […]

如何使用reflection将新项添加到集合中

我正在尝试使用reflection将未知对象添加到未知集合类型,并且当我实际执行“添加”时,我遇到exception。 我想知道是否有人可以指出我做错了什么或另类? 我的基本方法是迭代通过reflection检索的IEnumerable,然后将新项添加到第二个集合中,我稍后可以将其用作替换集合(包含一些更新的值): IEnumerable businessObjectCollection = businessObject as IEnumerable; Type customList = typeof(List) .MakeGenericType(businessObjectCollection.GetType()); var newCollection = (System.Collections.IList) Activator.CreateInstance(customList); foreach (EntityBase entity in businessObjectCollection) { // This is the area where the code is causing an exception newCollection.GetType().GetMethod(“Add”) .Invoke(newCollection, new object[] { entity }); } 例外是: “Eclipsys.Enterprise.Entities.Registration.VisitLite”类型的对象无法转换为“System.Collections.Generic.List`1 [Eclipsys.Enterprise.Entities.Registration.VisitLite]”类型。 如果我使用这行代码代替Add() ,我得到一个不同的exception: newCollection.Add(entity); 例外是: 值“”不是“System.Collections.Generic.List`1 [Eclipsys.Enterprise.Entities.Registration.VisitLite]”类型,并且不能在此通用集合中使用。