Compare. NET Objects Object Comparison components
Read Catalogue
- 1.Compare. NET Objects Introduction
- 2. Compare. NET Objects Considerations
- 3. A simple use case
- 4. Introduction of three Core objects
- 5. Resources
. NET Platform Open source project Quick glance today introduces a small and powerful object comparison component. The difference between 2 objects can be obtained in more detail, and the specific differences are recorded, and the comparison process and requirements can be flexibly configured.
This article address:. NET Platform Open Source project Quick glance (2) Compare. NET Objects Object comparison component
Back to directory 1.Compare. NET Objects Introduction
The Compare. NET objects component is an open-source component of the. NET platform for deep comparison of 2. NET objects, which has been updated, and the main features, as the name implies, are an in-depth comparison of 2. NET objects, whether they are equal or different. Probably a lot. NET object implements some. Net-brought interfaces that can be compared directly, but the scope and functionality of this component is more extensive. For example:
You can compare the default child nodes, and you can compare structures;
You can compare IList objects; You can compare single-dimensional or multidimensional array objects;
Enumeration types can be compared; IDictionary objects can be compared;
Can compare data sets, data tables, dictionaries, etc.;
You can compare private fields or properties, etc...
Compare. NET objects supports. NET 3.5 and later, and also supports Silverlight 5+, Windows Phone 8+, Windows RT 8+, Xamarin IOS, and Xamarin Androi D and other environments.
There are many, not listed, you can go to the official website to learn more. We focus on its basic use, and this article takes a few examples to get you closer to the world. Although simple, but more complex features may be used in different business scenarios, maybe you want the oh ... The only drawback to this component is that the case and documentation are not comprehensive, so I've researched the usage and knocked out some of the regular usage code to share it with you.
Official website: http://comparenetobjects.codeplex.com/
NuGet Package:http://www.nuget.org/packages/CompareNETObjects
Back to Directory 2. Compare. NET Objects Considerations
Before you better use compare. NET objects, there are a few things you need to know to make the most of your detour, and the main points of translation are the content of the official website:
1. For performance reasons, Compare. NET objects defaults to detecting only the first difference (you can confirm that it is not the same object), and if you want to compare multiple points, you need to manually set the maximum number of config.maxdifferences to the different points you need;
2. After the comparison is complete, the object difference points are obtained from the differences list or from the differencesstring attribute;
3. By default, it is a deep comparison, if only to perform a shallow comparison, you need to manually set the Comparechildren =false;
4. By default, private and private fields are not compared, and if necessary, the Config.compareprivateproperties and Config.compareprivatefields are set to true before comparison.
5. By default, if the comparison of the 2 objects of different types, will throw an exception, if you need to ignore this factor, you need to set config.ignoreobjecttypes=true;
It can be seen that the function of the component is not only perfect, but also relatively flexible, you want and do not want to be flexible to consider, by setting different switches to compare.
Back to Table of Contents 3. A simple use case
In order to make the following introduction of the source code clearer, first look at a simple use of the demo, after understanding the demo, we will in turn in-depth introduction of the comparison process of the three core objects. Understanding of these three core objects, the logic and use of the entire component is basically clear.
3.1 First define a person type
For a simple comparison, we define 1 person types, including the name, age, and 3 attributes of the creation date. As in the following code:
123456 |
public
class
Person
{
public
String Name {
get
;
set
; }
public
Int32 Age {
get
;
set
; }
public DateTime DateCreated {
get
;
set
; }
}
|
3.2 Comparison Test Demo
To first refer to the DLL of compare-net-objects, add the namespace reference in the demo after adding it:
1 |
using KellermanSoftware.CompareNetObjects; |
Next look at the core of the use process, look at the code comment:
1234567891011121314151617181920212223 |
//创建比较对象的类型
CompareLogic compareLogic =
new
CompareLogic();
//创建2个不同的Person类型
Person person1 =
new
Person();
person1.DateCreated = DateTime.Now;
person1.Name =
"Jorn"
;
person1.Age = 25;
Person person2 =
new
Person();
person2.Name =
"Greg"
;
person2.DateCreated = DateTime.Now;
person2.Age = 22;
//设置比较对象的配置文件,最大不同点为3
compareLogic.Config.MaxDifferences = 3;
//获取比较结果,使用Compare方法
ComparisonResult result = compareLogic.Compare(person1, person2);
//如果不相等,输出不同信息字符串
if
(!result.AreEqual)
Console.WriteLine(result.DifferencesString);
|
As shown in the above example, the whole process has 3 core related objects and points:
Initialization of the Comparelogic object;
The use of comparelogic configuration settings; Actually the settings of the Comparisonconfig class
Directly obtain the result, and output different information, is actually the use of comparisonresult.
The above is just a simple case, in order to learn more about the use, but also to see the role of these 3 main objects.
Back to Table of Contents 4. Introduction of three Core objects
Based on the examples and procedures in section 3rd, this section describes the 3 core types of structures that can be used faster before using them to understand their structure. Since this component does not provide a help document for the time being, I have translated the comments for these 3 core classes and have manually produced a CHM-formatted help document based on the comments in the article.
4.1 Comparelogic Comparison Logic class
Comparelogic is the main object of the comparison object. It consists of only 2 core things:
is to configure the property config, which is the most important setting before the comparison, and when Comparelogic is initialized, it can be initialized with the Comparisonconfig pass parameter, which makes it easier and in some cases does not require duplicate settings. It is possible to use a unified configuration. As the following is part of the source code:
12345678910111213141516171819 |
public
class
CompareLogic : ICompareLogic
{
/// <summary>默认的比较设置文件</summary>
public
ComparisonConfig Config {
get
;
set
; }
/// <summary>默认构造函数</summary>
public
CompareLogic()
{
Config =
new
ComparisonConfig();
}
/// <summary>使用外部的比较设置对象来进行初始化</summary>
/// <param name="config">外部设置对象</param>
public
CompareLogic(ComparisonConfig config)
{
Config = config;
}
..........
}
|
Comparison method. Returns a Comparisonresult object, where the process of designing to the core comparison method is not investigated.
So the above config attribute we need to be set according to our own requirements before executing the comparison method, such as setting the maximum number of different numbers and so on. The comparison method is simple, the core is in the type with the return, and then continues to look.
4.2 Comparisonconfig Comparison Configuration class
The comparison configuration class, where Comparelogic is a property, can be set before it is compared in a program. The core approach is to configure the project, for example, I put its core code out, look at it, at a glance, the component to achieve the different conditions for the comparison of the effect and function, which is done by this configuration.
1234567891011121314151617181920212223242526272829303132 |
/// <summary>
/// 时间日期类型不同(间隔)的最大毫秒数,默认为0:
/// 意思就是比较2个时间对象,差别在这个值以下,就认为是相同的,类似Double处理相等时的精度
/// </summary>
public
int
MaxMillisecondsDateDifference {
get
;
set
; }
/// <summary>结构体比较的最大深度(比较子节点),默认为2</summary>
public
int
MaxStructDepth {
get
;
set
; }
/// <summary>如果为true,遇到未知的对象类型时,将忽略,而不是直接抛出异常,默认为false,也就是抛出异常</summary>
public bool
IgnoreUnknownObjectTypes {
get
;
set
; }
/// <summary>如果为true,将跳过无效的索引。默认为false</summary>
public
bool
SkipInvalidIndexers {
get
;
set
; } /// <summary>在每个阶段比较后都显示记录,默认为false.这在调试很具有很深子节点的对象时非常有用</summary>
public
bool
ShowBreadcrumb {
get
;
set
; }
/// <summary>比较中需要忽略的类型列表.默认比较多有的类型</summary>
public
List<Type> ClassTypesToIgnore {
get
;
set
; }
/// <summary>只需要比较的类型列表。默认是比较多有类型,如果设置这个列表,那将只比较这个列表中的类型</summary>
public
List<Type> ClassTypesToInclude {
get
;
set
; }
/// <summary>比较期间需要忽略的数据表名称,或者表列名称,属性,或者字段。对大小写敏感</summary>
/// <example>MembersToIgnore.Add("CreditCardNumber")</example>
public
List<
string
> MembersToIgnore {
get
;
set
; }
/// <summary>只比较列表中的名称,如数据表,列名称,属性或者字段,大小写敏感。和上面的类型处理类似。</summary>
/// <example>MembersToInclude.Add("FirstName")</example>
public
List<
string
> MembersToInclude {
get
;
set
; }
...................
|
There are many other, detailed look at the source code I provide. I have translated several core classes and can use and understand them faster.
4.3 Comparisonresult comparison Result class
It is also important to compare the results, but it is too easy to simply return the equality and inequality, which is a different place for this functional component. It takes into account the results and different points, and can query the object at different points after the comparison is completed, or the direct string, and the values of the different points. With it, the main note is 3 properties:
123456 |
/// <summary> Compare the differences found </summary> public list<difference > differences { get ; set ;&NBSP;} /// <summary> compare different points, describe </summary> as strings; public string differencesstring{ get ;} /// <summary> returns TRUE</SUMMARY> if same; public bool areequal{ get ;} |
This is also useful in the demo code. When debugging, let's take a look, as shown in:
today's content is introduced here.
Back to catalog 5. Resources
You can download the source code from the official website: http://comparenetobjects.codeplex.com/.
My presentation demo and the translation section of the project, here also provides a download, the date is modified on the basis of the 2015-5-22 download.
my: Comparenetobjects-150525.rar
I manually made a partially translated component help document, such as:
: Compare.net components Chm.rar, children's shoes, download do not forget to praise oh ...
If you feel that reading this article is helpful to you, please click " recommend " button, your "recommendation" will be my biggest writing motivation! Welcome to reprint, but without the author's consent, reprint article must be in the article page obvious location to the author and the original link , otherwise reserve the right to pursue legal responsibility.
Compare. NET Objects Object Comparison components