C# 检查DependencyProperty是否已注册

C# 检查DependencyProperty是否已注册,c#,wpf,C#,Wpf,在我再次运行下面的代码之前,如何知道“Status”dependencProperty是否已注册 代码: 您将需要反射来找出已注册的从属属性 using System.Reflection; foreach (FieldInfo fInfo in CWindow.GetType().GetFields()) { if (fInfo.FieldType.Name == "DependencyProperty") { if (fInfo.GetValue(null)

在我再次运行下面的代码之前,如何知道“Status”dependencProperty是否已注册

代码:


您将需要反射来找出已注册的从属属性

using System.Reflection;

foreach (FieldInfo fInfo in CWindow.GetType().GetFields())
{
    if (fInfo.FieldType.Name == "DependencyProperty")
    {
        if (fInfo.GetValue(null) == "Status")
        {
            Console.WriteLine("Status Property already Registered");
        }
    }
}

我得到非静态场需要一个目标。如果(fInfo.GetValue(null)=“Status”)为cwindow创建了实例,则出现错误?这是控件吗?如果是,则将foreach中的cwindow替换为实例名称。由于我不知道您的实例名称,因此我使用了类型名称cwindow.use.GetType().GetFields()将cwindow更改为“this”。我以为是当前窗口。不,我仍然不明白你的意思。对不起。你包括反射代码了吗?你的朋友,这对你有用吗
using System.Reflection;

foreach (FieldInfo fInfo in CWindow.GetType().GetFields())
{
    if (fInfo.FieldType.Name == "DependencyProperty")
    {
        if (fInfo.GetValue(null) == "Status")
        {
            Console.WriteLine("Status Property already Registered");
        }
    }
}