Monday 15 July 2013

c# - Creating a generic DependencyProperty -



c# - Creating a generic DependencyProperty -

i have generic class takes template t should non-nullable object:

class point<t> t : struct { public t x; public t y; }

for reasons won't go here, need t struct, not object or class.

i want create usercontrol has instance of class dependencyproperty, example:

public class myusercontrol : usercontrol { static myusercontrol () {} public static readonly dependencyproperty pointdependencyproperty = dependencyproperty.register( "mypoint", typeof(point<???>), // problem! typeof(myusercontrol)); public point<object> mypoint { { homecoming (point<???>) getvalue(pointdependencyproperty ); } set { setvalue(pointdependencyproperty, value); } } }

as can seen in above code, don't know how register property. can done? tried object, nullable compiler tells me:

the type 'object' must non-nullable value type in order utilize parameter 't' in generic type or method 'mynamespace.point<t>'

making myusercontrol generic become problem different reasons, don't want go downwards route either. there way this?

this 1 should you. when can't because of strong typing, think containing can't do, in illustration mypoint contains varying type object , dp doesn't care.

public partial class myusercontrol : usercontrol { public myusercontrol() { initializecomponent(); var mp = new mypoint(); var mv = new mytype<string>("now time"); mp.mytype = mv; mypoint = mp; } public static readonly dependencyproperty pointdependencyproperty = dependencyproperty.register( "mypoint", typeof(mypoint), // problem! typeof(myusercontrol)); public mypoint mypoint { { homecoming (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; } }

c# generics dependency-properties

No comments:

Post a Comment