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

问题描述

我有必要梳理这些基于在运行时确定的标准集合



我用的是代码的文章进行排序 - 原来我的代码中使用的动态类



然后我打的问题与系列化超过WCF所以我切换到使用的,现在上线的排序代码游:

 的PropertyInfo PI = type.GetProperty (支柱); 



与错误SerializableDynamicObject没有一个所谓的名称属性 - 在名是道具的价值。



我想这样做最简单的事情是要找到一个序列化动态类型的排序算法与工作的另一种方式。在这个方向的任何指针将不胜感激!



我已经看过的的例子,但我得到的错误信息:

 带参数(SerializationInfo中,的StreamingContext构造)不ISerializable的类型
发现


解决方案

下面是一个使用一些代码了解这一点,这同时适用于基于反射和动态为主的对象(取决于你传递给 TypeAccessor.Create 什么)

 使用系统; System.Collections中使用
;
使用System.Collections.Generic;
使用System.Dynamic;
使用FastMember;

命名空间ConsoleApplication6
{
类节目
{
静态无效的主要()
{
无功名单=新名单,LT;动态>();
动态的obj =新ExpandoObject();
obj.Foo = 123;
obj.Bar =XYZ;
list.Add(OBJ);
OBJ =新ExpandoObject();
obj.Foo = 456;
obj.Bar =高清;
list.Add(OBJ);
OBJ =新ExpandoObject();
obj.Foo = 789;
obj.Bar =ABC;
list.Add(OBJ);

VAR访问= TypeAccessor.Create(
typeof运算(IDynamicMetaObjectProvider));
字符串作为propName =酒吧;
list.Sort((X,Y)=> Comparer.Default.Compare(
存取[X,的propName],存取[Y,的propName]));

的foreach(在列表VAR项){
Console.WriteLine(item.Bar);
}
}
}
}



据可能是值得mentioining,对于基于反射的类型,这样做的不可以在每个项目基础上使用反射;所有通过元编程优化掉。


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动态排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 19:12