使用SerializableDynamicObject进行动态排

使用SerializableDynamicObject进行动态排

本文介绍了使用SerializableDynamicObject进行动态排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用,现在排序代码中断行:

  PropertyInfo pi = type。的getProperty(丙); 

与SerializableDynamicObject没有名为Name的属性的错误 - 其中Name是价值的道具



我想最简单的事情是找到序列化排序算法的动态类型的替代方法。任何指向此方向的指针都将不胜感激!



我看过例子,但是我收到错误信息:

 具有参数的构造函数(SerializationInfo,StreamingContext )在ISerializable类型中没有找到


解决方案

,适用于基于反射和动态基于对象(取决于你传递给 TypeAccessor.Create

  using System; 
使用System.Collections;
使用System.Collections.Generic;
使用System.Dynamic;
使用FastMember;

命名空间ConsoleApplication6
{
类程序
{
static void Main()
{
var list = new List&动态>();
dynamic obj = new ExpandoObject();
obj.Foo = 123;
obj.Bar =xyz;
list.Add(obj);
obj = new ExpandoObject();
obj.Foo = 456;
obj.Bar =def;
list.Add(obj);
obj = new ExpandoObject();
obj.Foo = 789;
obj.Bar =abc;
list.Add(obj);

var accessor = TypeAccessor.Create(
typeof(IDynamicMetaObjectProvider));
string propName =Bar;
list.Sort((x,y)=> Comparer.Default.Compare(
accessor [x,propName],accessor [y,propName]));

foreach(列表中的var项目){
Console.WriteLine(item.Bar);
}
}
}
}

它可能值得注意的是,对于基于反射的类型,这不是在每个项目的基础上使用反射;所有这些都通过元编程进行了优化。


I have a need to sort a collection of these based upon criteria determined at run-time.

I was using the code from this article to perform the sorting - originally my code used the dynamic class.

Then I hit issues with serialization over WCF so I switched to using a SerializableDynamicObject and now the sorting code breaks on the line:

  PropertyInfo pi = type.GetProperty(prop);

with the error that SerializableDynamicObject does not have a property called "Name" - where "Name" was the value of prop.

I guess the simplest thing to do is to find an alternate way of serializing a dynamic type that the sorting algorithm works with. Any pointers in this direction would be appreciated!

I have looked at this example, but I get the error message:

The constructor with parameters (SerializationInfo, StreamingContext) is not found in ISerializable type
解决方案

Here's some code using FastMember for this, which works for both reflection-based and dynamic-based objects (depending on what you pass to TypeAccessor.Create)

using System;
using System.Collections;
using System.Collections.Generic;
using System.Dynamic;
using FastMember;

namespace ConsoleApplication6
{
    class Program
    {
        static void Main()
        {
            var list = new List<dynamic>();
            dynamic obj = new ExpandoObject();
            obj.Foo = 123;
            obj.Bar = "xyz";
            list.Add(obj);
            obj = new ExpandoObject();
            obj.Foo = 456;
            obj.Bar = "def";
            list.Add(obj);
            obj = new ExpandoObject();
            obj.Foo = 789;
            obj.Bar = "abc";
            list.Add(obj);

            var accessor = TypeAccessor.Create(
                typeof(IDynamicMetaObjectProvider));
            string propName = "Bar";
            list.Sort((x,y) => Comparer.Default.Compare(
                accessor[x, propName], accessor[y,propName]));

            foreach(var item in list) {
                Console.WriteLine(item.Bar);
            }
        }
    }
}

It may be worth mentioining that for reflection-based types, this does not use reflection on a per-item basis; all that is optimized away via meta-programming.

这篇关于使用SerializableDynamicObject进行动态排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 00:05