惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
aimingoo的专栏
aimingoo的专栏
P
Proofpoint News Feed
宝玉的分享
宝玉的分享
MyScale Blog
MyScale Blog
The GitHub Blog
The GitHub Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
月光博客
月光博客
量子位
博客园 - 司徒正美
V
V2EX
I
InfoQ
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Vercel News
Vercel News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
美团技术团队
N
Netflix TechBlog - Medium
L
LangChain Blog
IT之家
IT之家
Blog — PlanetScale
Blog — PlanetScale
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Stack Overflow Blog
Stack Overflow Blog
A
About on SuperTechFans
Microsoft Azure Blog
Microsoft Azure Blog

博客园 - 杨林

IIs一些配置 扩大传输限制 exchange file 关于VS2005 无法使用切换到设计视图的解决方法 使用ASP.Net Forms模式实现WebService身份验证 SendEmail - 杨林 - 博客园 web导出excel格式问题 Form身份验证 - 杨林 - 博客园 在ASP.NET 2.0中使用Membership sql T 设计模式之状态模式 sqlserver2005学习笔记 共享内存进程之间 sql 利用索引优化性能 SQL 2005新增的几个函数之学习 Thread pool web service web service 多线程分析 js Function.call
ComparaCarAdapter
杨林 · 2008-01-03 · via 博客园 - 杨林

但是这样如果属性很多,会产生很多的类,怎么办呢。那么利用反射吧。将ComparaCarAdapter改造为:

    public class ComparaCarAdapter : IComparer<Car>
    
{
        
string _progName = "";
        
public ComparaCarAdapter(string progName)
        
{
            _progName 
= progName;
        }

        
#region IComparer<Employee> 成员

        
public int Compare(Car x, Car y)
        
{
            Type t 
= typeof(Car);
            PropertyInfo pi 
= t.GetProperty(_progName);            
            
object xvalue = pi.GetValue(x, null);
            
object yvalue = pi.GetValue(y, null);
            
if (xvalue is string)
            
{
                
return ((string)xvalue).CompareTo((string)yvalue);
            }

            
else
            
{
                
if (xvalue is int)
                    
return ((int)xvalue).CompareTo((int)yvalue);
            }

            
throw new NotSupportedException();
        }


        
#endregion

    }

调用

Array.Sort<Car>(arr, new ComparaCarAdapter("Weight"));