我正在尝试从DependencyProperty中清除GeometryGroup值,因为内部静态readonly无法清除值。
internal GeometryGroup CurrentGeometry
{
get { return (GeometryGroup)GetValue(CurrentGeometryProperty); }
set { SetValue(CurrentGeometryProperty, value); }
}
internal static readonly DependencyProperty CurrentGeometryProperty =
DependencyProperty.Register(
"CurrentGeometry",
typeof(GeometryGroup),
typeof(ShapeEditorControl),
new FrameworkPropertyMetadata(default(GeometryGroup), CurrentGeometryChanged));
这是注册DependencyProperty的方式,请检查以下函数以取消注册DependencyProperty。
public void RemoveDependency()
{
DependencyProperty prop = CurrentGeometryProperty;
var registeredPropertyField = typeof(DependencyProperty).
GetField("RegisteredPropertyList", BindingFlags.NonPublic | BindingFlags.Static);
object list = registeredPropertyField.GetValue(null);
var genericMeth = list.GetType().GetMethod("Remove");
try
{
genericMeth.Invoke(list, new[] { prop });
}
catch (TargetInvocationException)
{
Console.WriteLine("Does not exist in list");
}
var propertyFromNameField = typeof(DependencyProperty).
GetField("PropertyFromName", BindingFlags.NonPublic | BindingFlags.Static);
var propertyFromName = (Hashtable)propertyFromNameField.GetValue(null);
object keyToRemove = null;
foreach (DictionaryEntry item in propertyFromName)
{
if (item.Value == prop)
keyToRemove = item.Key;
}
if (keyToRemove != null)
propertyFromName.Remove(keyToRemove);
}
请指导我取消设置DependencyProperty并清除GeometryGroup值。