我正试图创建一个通用类型来处理我在C#实体之间的所有引用。
body {
counter-reset: section; /* Set a counter named 'section', and its initial value is 0. */
}
h3::before {
counter-increment: section; /* Increment the value of section counter by 1 */
content: counter(section); /* Display the value of section counter */
}
仅仅是reference
的ID,因此记住它在哪里使用ans实体的正确数据结构将是int
HashSet<int>
ID和作为对象集合的路径。适当的数据结构应为int
这是我到目前为止所拥有的:
Dictionary<int, Tvalue>
有没有办法使第二个public class References<TValue> : Dictionary<int, TValue>
{
}
public class References : HashSet<int>
{
}
public class MyEntityWithDefaultReferences
{
public References References;
}
public class MyEntityWithPathReferences<PathType>
{
public References<PathType> References;
}
类从第一个继承?所以我可以在任何地方使用父类。
答案 0 :(得分:0)
好吧,这样
public interface MyReference{
int Id{get;set;}
}
public class SimpleReference : MyReference
{
public int Id{get;set;}
}
public class CompoundReference<T> : MyReference
{
public int Id{get;set;}
public T TValue{get;set;}
}
public class ReferenceCollection : KeyedCollection<int, MyReference>
{
protected override int GetKeyForItem(MyReference item)
{
return item.Id;
}
}
您可以使用
SimpleReference sr = new SimpleReference(){Id=1};
CompoundReference<string> cr = new CompoundReference<string>(){Id=2,TValue="Test"};
ReferenceCollection col = new ReferenceCollection();
col.Add(sr);
col.Add(cr);
Console.WriteLine(col.Contains(1)); //true
Console.WriteLine(col.Contains(2)); //true
Console.WriteLine(col.Contains(3)); //false
var i1 = col[1]; //returns sr
var i2 = col[2]; //return cr