gpt4 book ai didi

c# - 有没有更好的方法来 self 引用类型?

转载 作者:行者123 更新时间:2023-11-30 16:35:52 25 4
gpt4 key购买 nike

我最近发现很多与在 C# 中引用泛型类相关的代码味道。我的提示尤其适用于那些继承自 DependencyObject 并包含 DependencyProperties 的类。

基本问题是,当声明依赖属性时,通常会引用当前类型,也称为所有者。这工作得很好,一般来说对于简单的非泛型对象来说不是什么大问题,除非对象包含多个依赖属性,然后类型名称需要在不同的地方重构(重构更容易 Visual Studio )。

public class MyDependencyObject : DependencyObject
{
public int MyProperty
{
get { return (int)GetValue(MyPropertyProperty); }
set { SetValue(MyPropertyProperty, value); }
}

public static readonly DependencyProperty MyPropertyProperty =
DependencyProperty.Register("MyProperty", typeof(int), typeof(MyDependencyObject), new UIPropertyMetadata(0));
}

不过我最近发现的是,当将这种繁琐的显式自引用实践与泛型的广泛使用结合起来时,代码真的开始变得丑陋了。

public class MyDependencyObject<TypeA, TypeB, TypeC, TypeD> : DependencyObject
{
public int MyProperty1
{
get { return (int)GetValue(MyPropertyProperty1); }
set { SetValue(MyPropertyProperty1, value); }
}

public static readonly DependencyProperty MyPropertyProperty1 =
DependencyProperty.Register("MyProperty1", typeof(int), typeof(MyDependencyObject<TypeA, TypeB, TypeC, TypeD>));

public int MyProperty2
{
get { return (int)GetValue(MyPropertyProperty2); }
set { SetValue(MyPropertyProperty2, value); }
}

public static readonly DependencyProperty MyPropertyProperty2 =
DependencyProperty.Register("MyProperty2", typeof(int), typeof(MyDependencyObject<TypeA, TypeB, TypeC, TypeD>));

public int MyProperty3
{
get { return (int)GetValue(MyPropertyProperty3); }
set { SetValue(MyPropertyProperty3, value); }
}

public static readonly DependencyProperty MyPropertyProperty3 =
DependencyProperty.Register("MyProperty3", typeof(int), typeof(MyDependencyObject<TypeA, TypeB, TypeC, TypeD>));

public int MyProperty4
{
get { return (int)GetValue(MyPropertyProperty4); }
set { SetValue(MyPropertyProperty4, value); }
}

public static readonly DependencyProperty MyPropertyProperty4 =
DependencyProperty.Register("MyProperty4", typeof(int), typeof(MyDependencyObject<TypeA, TypeB, TypeC, TypeD>));
}

我的问题是,是否有人知道任何技巧、黑客或合法解决方案,以减少在上述情况下需要引用带有泛型参数的完整类型名称的次数。

完全披露:我确实在 Microsoft .Connect 网站上将此作为问题提出,但他们拒绝了 self referencing keyword 的想法。但没有提供解决方法或替代解决方案。我的想法是使用一些关键字,例如 OwnerOwnerClassThisType,以便一般性地引用关键字所在的类型用过。

最佳答案

您可以采取一些措施来缓解疼痛。创建一个静态变量,其中包含当前类的类型信息。

private static readonly Type ThisType = typeof(MyDependencyObject<TypeA, TypeB, TypeC, TypeD>));

public int MyProperty1
{
get { return (int)GetValue(MyPropertyProperty1); }
set { SetValue(MyPropertyProperty1, value); }
}

public static readonly DependencyProperty MyPropertyProperty1 =
DependencyProperty.Register("MyProperty1", typeof(int), ThisType);

接下来你可以使用this使对 getter 和 setter 的引用重构安全的巧妙技巧。

private static string GetPropertyName<TSource, TResult>(Expression<Func<TSource, TResult>> expression)
{
if (expression.NodeType == ExpressionType.Lambda && expression.BodyType == ExpressionType.MemberAccess)
{
PropertyInfo pi = (expression.Body as MemberExpression).Member as PropertyInfo;
if (pi != null)
{
return pi.Name;
}
}
throw new ArgumentException("expression", "Not a property expression.");
}

现在您的代码会像这样。

private static readonly Type ThisType = typeof(MyDependencyObject<TypeA, TypeB, TypeC, TypeD>));

public int MyProperty1
{
get { return (int)GetValue(MyPropertyProperty1); }
set { SetValue(MyPropertyProperty1, value); }
}

public static readonly DependencyProperty MyPropertyProperty1 =
DependencyProperty.Register(GetPropertyName((MyDependencyObject x) => x.MyProperty1), typeof(int), ThisType);

关于c# - 有没有更好的方法来 self 引用类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1462382/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com