C# 创建泛型DependencyProperty

C# 创建泛型DependencyProperty,c#,generics,dependency-properties,C#,Generics,Dependency Properties,我有一个泛型类,它接受一个模板T,它应该是一个不可为空的对象: class Point<T> where T : struct { public T x; public T y; } 从上面的代码中可以看出,我不知道如何注册这个属性。甚至可以做到吗?我尝试了对象,但这是可以为空的,因此编译器告诉我: The type 'object' must be a non-nullable value type in order to use it as parameter

我有一个泛型类,它接受一个模板
T
,它应该是一个不可为空的对象:

class Point<T> where T : struct
{
    public T x;
    public T y;
}
从上面的代码中可以看出,我不知道如何注册这个属性。甚至可以做到吗?我尝试了
对象
,但这是可以为空的,因此编译器告诉我:

The type 'object' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'MyNamespace.Point<T>'
类型“object”必须是不可为null的值类型,才能将其用作泛型类型或方法“MyNamespace.Point”中的参数“T”

出于不同的原因,使
MyUserControl
通用将成为一个问题,因此我也不想走这条路。有办法做到这一点吗?

这是因为您将
T
定义为一个
struct
或派生它的东西

如果将
替换为
结构
,如
,则它可以工作:

public Point<DateTime> MyPoint
{
    get { return (Point<DateTime>) GetValue(PointDependencyProperty ); }
    set { SetValue(PointDependencyProperty, value); }
}

这意味着
T
可以是任何东西,您可以按照习惯的方式访问它,而不存在仅使用
对象
动态
的缺点。这一个应该可以为您完成。如果由于强类型而无法执行某些操作,请考虑包含无法执行的操作,在本例中,MYPOINT包含可变类型对象,而DP不关心

public partial class MyUserControl : UserControl
{
    public MyUserControl()
    {
        InitializeComponent();
        var mp = new MyPoint();
        var mv = new MyType<string>("Now is the time");
        mp.MyType = mv;
        MyPoint = mp;
    }

    public static readonly DependencyProperty PointDependencyProperty =
        DependencyProperty.Register(
          "MyPoint",
          typeof(MyPoint),  // This is the problem!
          typeof(MyUserControl));

    public MyPoint MyPoint
    {
        get { return (MyPoint)GetValue(PointDependencyProperty); }
        set { SetValue(PointDependencyProperty, value); }
    }
}

public class MyPoint
{
    public dynamic MyType { get; set; }
}

public class MyType<T>
{
    public dynamic Myvalue { get; set; }

    public Point MyPoint { get; set; }

    public MyType(T value)
    {
        Myvalue = value;
    }
}
公共部分类MyUserControl:UserControl
{
公共MyUserControl()
{
初始化组件();
var mp=new MyPoint();
var mv=新的MyType(“现在是时候了”);
mp.MyType=mv;
MyPoint=mp;
}
公共静态只读DependencyProperty点DependencyProperty=
从属属性。寄存器(
“MyPoint”,
typeof(MyPoint),//这就是问题所在!
typeof(MyUserControl));
公共MyPoint MyPoint
{
获取{return(MyPoint)GetValue(pointdependencProperty);}
set{SetValue(pointdependencProperty,value);}
}
}
公共类MyPoint
{
公共动态MyType{get;set;}
}
公共类MyType
{
公共动态Myvalue{get;set;}
公共点MyPoint{get;set;}
公共MyType(T值)
{
我的价值=价值;
}
}

很好,我应该提到这一点,但是:是的,我确实需要
T
成为一个结构。然后为它提供一个
struct
object
不是一个
struct
。您能更具体一点吗?因为
typeof(Point)
不是法定代码。没错,它需要更具体,比如
DateTime
,或者任何其他结构。我想你的要求是不可能的。模板T应该是不可为空的对象:“,而
T
struct
class Point<T>
{
}
public partial class MyUserControl : UserControl
{
    public MyUserControl()
    {
        InitializeComponent();
        var mp = new MyPoint();
        var mv = new MyType<string>("Now is the time");
        mp.MyType = mv;
        MyPoint = mp;
    }

    public static readonly DependencyProperty PointDependencyProperty =
        DependencyProperty.Register(
          "MyPoint",
          typeof(MyPoint),  // This is the problem!
          typeof(MyUserControl));

    public MyPoint MyPoint
    {
        get { return (MyPoint)GetValue(PointDependencyProperty); }
        set { SetValue(PointDependencyProperty, value); }
    }
}

public class MyPoint
{
    public dynamic MyType { get; set; }
}

public class MyType<T>
{
    public dynamic Myvalue { get; set; }

    public Point MyPoint { get; set; }

    public MyType(T value)
    {
        Myvalue = value;
    }
}