C# 处理通用树键
我需要处理树的通用接口。例如:C# 处理通用树键,c#,generics,interface,C#,Generics,Interface,我需要处理树的通用接口。例如: class ITreeNode<TKey> { TKey Id {get; set;} TKey? ParentId {get; set;} // okay for numeric types, but what about strings? } class ITreeNode<TKey> { TKey Id {get; set;} TKey ParentId {get; set;} // again ok
class ITreeNode<TKey>
{
TKey Id {get; set;}
TKey? ParentId {get; set;} // okay for numeric types, but what about strings?
}
class ITreeNode<TKey>
{
TKey Id {get; set;}
TKey ParentId {get; set;} // again okay for strings, but what about numeric types?
}
类ITreeNode
{
TKey Id{get;set;}
TKey?ParentId{get;set;}//对于数字类型可以,但是字符串呢?
}
类ITreeNode
{
TKey Id{get;set;}
TKey ParentId{get;set;}//字符串也可以,但是数字类型呢?
}
如果泛型类型参数必须支持基本类型和类(如
string
),如何处理属性的类型?实体是使用实体框架保存到数据库中的,所以我不能做像bool HasParent
(外键)这样的事情。我会通过使用三个接口来实现这一点:一个非泛型变量,一个泛型用于基本类型(如int
,bool
,等等),另一个用于其余(类)
一些代码:
interface ITreeNodeNG
{
object Id { get; set; }
object ParentId { get; set; }
}
interface ITreeNodeP<TKey> where TKey : struct
{
TKey? Id { get; set; }
TKey? ParentId { get; set; }
}
interface ITreeNodeC<TKey>
{
TKey Id { get; set; }
TKey ParentId { get; set; }
}
public class X : ITreeNodeP<int>, ITreeNodeNG
{
public int? Id { get; set; }
public int? ParentId { get; set; }
object ITreeNodeNG.Id
{
get
{
return this.Id;
}
set
{
this.Id = (int)value;
}
}
object ITreeNodeNG.ParentId
{
get
{
return this.ParentId;
}
set
{
this.ParentId = (int?)value;
}
}
}
static void Main(string[] args)
{
ITreeNodeNG x = new X();
ITreeNodeP<int> y = new X();
ITreeNodeC<string> z = null; // you know what to do
}
接口ITreeNode
{
对象Id{get;set;}
对象ParentId{get;set;}
}
接口ITreeNodeP,其中TKey:struct
{
TKey?Id{get;set;}
TKey?ParentId{get;set;}
}
接口ITreeNodeC
{
TKey Id{get;set;}
TKey ParentId{get;set;}
}
公共类X:ITreeNode,ITreeNode
{
公共int?Id{get;set;}
public int?ParentId{get;set;}
对象ITreeNode.Id
{
得到
{
返回此.Id;
}
设置
{
这个.Id=(int)值;
}
}
对象ITreeNodeg.ParentId
{
得到
{
返回此.ParentId;
}
设置
{
this.ParentId=(int?)值;
}
}
}
静态void Main(字符串[]参数)
{
ITreeNodeNG x=新x();
ITreeNodeP y=新的X();
ITreeNodeC z=null;//您知道该怎么做
}
示例类
X
实现了非泛型接口和基元接口。这将使您能够在不知道接口的确切类型的情况下获取ID。如果您愿意,您的X
类可以是一个(抽象)基类。我猜您可能想要类似的类-它相当于null
,但它也适用于引用类型
public interface ITreeNode<TKey>
{
TKey Id { get; set; }
Maybe<TKey> ParentId { get; set; }
}
“如何应对”是一个相当广泛的问题。你面临什么问题?你想要的是等价于Nullable
,但同样适用于引用类型的东西吗?谢谢,objectnodeid
对我来说已经足够了。显然,我的思维太强了,无法理解;)
public class Maybe<T>
{
public readonly static Maybe<T> Nothing = new Maybe<T>();
public T Value { get; private set; }
public bool HasValue { get; private set; }
public Maybe()
{
HasValue = false;
}
public Maybe(T value)
{
Value = value;
HasValue = true;
}
public static implicit operator Maybe<T>(T v)
{
return v.ToMaybe();
}
}
public class TreeNode<TKey> : ITreeNode<TKey>
{
public TreeNode(TKey id)
: this(id, Maybe<TKey>.Nothing)
{ }
public TreeNode(TKey id, Maybe<TKey> parentId)
{
this.Id = id;
this.ParentId = parentId;
}
public TKey Id { get; set; }
public Maybe<TKey> ParentId { get; set; }
}
ITreeNode<int> a = new TreeNode<int>(5);
ITreeNode<int> b = new TreeNode<int>(5, 1);
ITreeNode<string> c = new TreeNode<string>("q1");
ITreeNode<string> d = new TreeNode<string>("q2", "q1");
public static class MaybeEx
{
public static Maybe<T> ToMaybe<T>(this T value)
{
return new Maybe<T>(value);
}
public static Maybe<U> Select<T, U>(this Maybe<T> m, Func<T, U> k)
{
return m.SelectMany(t => k(t).ToMaybe());
}
public static Maybe<U> SelectMany<T, U>(this Maybe<T> m, Func<T, Maybe<U>> k)
{
if (!m.HasValue)
{
return Maybe<U>.Nothing;
}
return k(m.Value);
}
public static Maybe<V> SelectMany<T, U, V>(this Maybe<T> @this, Func<T, Maybe<U>> k, Func<T, U, V> s)
{
return @this.SelectMany(x => k(x).SelectMany(y => s(x, y).ToMaybe()));
}
}