C# 为对象创建一致的Guid
我们必须为对象创建一致的Guid。我正在考虑使用下面的方法为具有相同属性的对象创建唯一的guid,但不知怎么的,它感觉是错误的C# 为对象创建一致的Guid,c#,.net,oop,uuid,C#,.net,Oop,Uuid,我们必须为对象创建一致的Guid。我正在考虑使用下面的方法为具有相同属性的对象创建唯一的guid,但不知怎么的,它感觉是错误的 class Person { public string Name { get; set; } public int Age { get; set; } public override string ToString() { return "Person: " + Name + " " + Age; } } /
class Person
{
public string Name { get; set; }
public int Age { get; set; }
public override string ToString()
{
return "Person: " + Name + " " + Age;
}
}
// Below tool is taken from http://stackoverflow.com/questions/2642141/how-to-create-deterministic-guids
Guid guid = GuidUtility.Create(GLOBAL, new Person(..).ToString();
我想要
我想的另一个可能的解决方案是在持久性存储中保存一个映射,每次创建一个新对象时,都要查看具有相同属性的对象是否已经存在
RFC 4122保证每个名称空间和名称都具有确定性和唯一性。
有什么建议吗。谢谢我在这个答案上使用了哈希算法。显然,对于具有相同哈希代码的不同值对象,始终存在冲突问题。但这个问题是无法解决的,因为您实际上是在从一个潜在的无限域转换到一个非常有限的范围,大部分是32位,对于Guid,是128位。解决这类问题的唯一办法是实现一个查找表,尽管它发生的可能性很低,如您所说
public static class ConsistentGuid
{
public static System.Guid Generate(object obj)
{
var bytes = new byte[16];
var type = obj.GetType();
var features = new object[]
{
type,
obj
};
BitConverter.GetBytes(LongHash(features))
.CopyTo(bytes, 0);
var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
features = new object[properties.Length];
for (int i = 0; i < properties.Length; i++)
features[i] = properties[i].GetValue(obj);
BitConverter.GetBytes(LongHash(features))
.CopyTo(bytes, 8);
return new System.Guid(bytes);
}
public static int Hash(object[] features, uint seed = 2166136261)
{
// https://stackoverflow.com/questions/263400/what-is-the-best-algorithm-for-an-overridden-system-object-gethashcode/263416#263416
unchecked // Overflow is fine, just wrap
{
int hash = (int)seed;
for (int i = 0; i < features.Length; i++)
{
if (features[i] == null) // Suitable nullity checks etc, of course :)
continue;
hash = (hash * 16777619) ^ features[i].GetHashCode();
}
return hash;
}
}
private static long LongHash(object[] features, ulong seed = 2166136261)
{
// https://stackoverflow.com/questions/263400/what-is-the-best-algorithm-for-an-overridden-system-object-gethashcode/263416#263416
unchecked // Overflow is fine, just wrap
{
long hash = (long)seed;
for (int i = 0; i < features.Length; i++)
{
if (features[i] == null) // Suitable nullity checks etc, of course :)
continue;
hash = (hash * 16777619) ^ features[i].GetHashCode();
}
return hash;
}
}
}
我不明白你为什么要使用Guid。换句话说,你为什么要使用Guid?
public class ConsistentGuidTests
{
[Fact]
public void Referencewise_Equal_Objects_Should_Generate_Same_Guids()
{
var obj = new object();
var obj2 = obj;
var guid1 = ConsistentGuid.Generate(obj);
var guid2 = ConsistentGuid.Generate(obj2);
Assert.True(ReferenceEquals(obj, obj2));
Assert.Equal(guid1, guid2);
}
[Fact]
public void ValueObjects_Of_DifferentTypes_Should_Generate_Different_Guids()
{
var obj = new object();
var other = new int();
var guid1 = ConsistentGuid.Generate(obj);
var guid2 = ConsistentGuid.Generate(other);
Assert.NotEqual(guid1, guid2);
}
[Fact]
public void ValueObjects_With_Same_Values_Should_Generate_Same_Guids()
{
var obj = 123;
var other = 123;
var guid1 = ConsistentGuid.Generate(obj);
var guid2 = ConsistentGuid.Generate(other);
Assert.False(ReferenceEquals(obj, other));
Assert.Equal(guid1, guid2);
}
[Fact]
public void ValueObjects_With_Different_Values_Should_Generate_Different_Guids()
{
var obj = 123;
var other = 124;
var guid1 = ConsistentGuid.Generate(obj);
var guid2 = ConsistentGuid.Generate(other);
Assert.NotEqual(guid1, guid2);
}
class AReferenceType
{
public int SomeProperty { get; set; }
public string SomeOtherProperty { get; set; }
public AReferenceType(int a, string b)
{
SomeProperty = a;
SomeOtherProperty = b;
}
public override int GetHashCode()
{
return ConsistentGuid.Hash(new object[]
{
SomeProperty,
SomeOtherProperty
});
}
}
[Fact]
public void ReferenceObjects_With_Same_Values_Should_Generate_Same_Guids()
{
var a = 123;
var b = "asd";
var obj = new AReferenceType(a, b);
var other = new AReferenceType(a, b);
var guid1 = ConsistentGuid.Generate(obj);
var guid2 = ConsistentGuid.Generate(other);
Assert.False(ReferenceEquals(obj, other));
Assert.Equal(obj.GetHashCode(), other.GetHashCode());
Assert.Equal(guid1, guid2);
}
[Fact]
public void ReferenceObjects_With_Different_Values_Should_Generate_Different_Guids()
{
var a = 123;
var b = "asd";
var obj = new AReferenceType(a, b);
var other = new AReferenceType(a + 1, b);
var guid1 = ConsistentGuid.Generate(obj);
var guid2 = ConsistentGuid.Generate(other);
Assert.NotEqual(guid1, guid2);
}
}