누군가이를 반영하기 위해 제안했습니다. 제가 사용한 방법은 훌륭하지만, 80 만회 이상의 반복 작업을 통해, 반사가 단지 그것을 잘라 내지 않는다는 명백한 결론에 도달했습니다.두 객체의 속성을 비교하는 가장 빠른 방법
public static class Helper
{
public static string[] ignoredProperties = { "EntityState",
"EntityKey",
"Prop1",
"Prop2",
"Whatever",
};
/// <summary>
/// Check if properties of two objects are the same. Bypasses specified properties.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="first"></param>
/// <param name="other"></param>
/// <param name="ignoreProperties"></param>
/// <returns></returns>
public static bool PropertiesEquals<T>(this T first, T other, string[] ignoreProperties)
{
var propertyInfos = first.GetType().GetProperties();
foreach (PropertyInfo propertyInfo in propertyInfos)
{
//Faster with custom method ? Nah...
//if (FindElementIndex(ignoreProperties, propertyInfo.Name) < 0)
//Probably faster if hardcoded.... Nah, not really either...
//if (propertyInfo.Name != "EntityKey" && propertyInfo.Name != "EntityState" && propertyInfo.Name != "Group_ID" && propertyInfo.Name != "Import_status")
if (Array.IndexOf(ignoreProperties, propertyInfo.Name) < 0)
if (!Equals(propertyInfo.GetValue(first, null), propertyInfo.GetValue(other, null)))
return false;
}
return true;
}
public static int FindElementIndex(string[] input, string value)
{
int arraySize = input.Length - 1;
Type valueType = value.GetType();
for (int x = 0; x <= arraySize; x++)
{
if (input[x] == value)
return x;
}
return -1;
}
문제는 종류에 따라 그 개체를 확인하기 위해 최대 50 개 특성을 가질 수 있다는 것입니다 :
여기 내 도우미 클래스의 부분입니다. 어 ... 그래서 나는 그럴 수는 없다.
속도를 조금 높일 수있는 방법이 있습니까?
감사합니다.
해결 방법 : Equals 메서드를 사용하고 각 클래스에 대해이를 재정의하여 속성 비교를 구현합니다 (반사 없음). – Keith