gpt4 book ai didi

c# - 传递属性并上课

转载 作者:行者123 更新时间:2023-11-30 13:54:56 25 4
gpt4 key购买 nike

这可能是一个微不足道的问题,但我在这里画了一个空白,似乎无法在网上找到答案。

基本上,我正在尝试创建一个方法,该方法将属于 INotifyPropertyChanged 类的 2 个属性作为参数(要在反射中使用的实际属性,而不是属性值),并保持它们像绑定(bind)一样“同步”。

例子

我有一个名为 Student 的类,它有一个名为 int SemesterScore 的属性。我有另一个名为 Semester 的类(class),它有一个名为 int Score 的属性。这两个类都实现了 IPropertyNotifyChanged

现在,让我们暂时假设我们不能扩展任何类(就像在我的现实生活场景中一样)并且我可能在不同的类中多次想要使用它。

基本上,我希望能够在我的一个类中调用一个方法,将两个属性“链接”在一起。也就是说,如果其中一个发生更改,它将自动更新另一个。

在非工作代码中,这是基本概念:

public class Student : INotifyPropertyChanged
{
private int _semesterScore;
public int SemeseterScore
{
get { return _semesterScore; }
set { [ set property stuff with property changed] }
}
}

public class Semester: INotifyPropertyChanged
{
private int _score;
public int Score
{
get { return _score; }
set { [ set property stuff with property changed] }
}
}

public class Entry
{
public static void Main(string[] args)
{
Student student = new Student();
Semester semester = new Semester();

AttachProperties(student.SemesterScore, semester.Score); // This obviously won't work, but this is where I pass the properties in

semester.Score = 7;
Console.WriteLine(student.SemesterScore); // Output will be 7
}

public static void AttachProperties([sometype] prop1, [sometype] prop2)
{
// Sudo code
prop1.classInstance.PropertyChanged += (pe)
{
if (pe.Property == prop1.Name)
prop2.Value = prop1.Value;
}

prop2.classInstance.PropertyChanged += (pe)
{
if (pe.Property == prop2.Name)
prop1.Value = prop2.Value;
}
}
}

有什么办法吗?我知道一些解决方法(也就是传递 INotifyPropertyChanged 类和属性名称,然后进行一些反射(reflection)以使其起作用),但是在我的编码中出现了几次传递属性实例(并用它做事)的问题事业。

最佳答案

一种方法是使用 Observable ,就像上面建议的@itay-podhacer。

但是如果你只想使用反射和 INotifyPropertyChanged 来实现这是您可以做到的。

首先,让我们得到 SemesterScoreStudent两者都实现 INotifyPropertyChanged :

public class Student : INotifyPropertyChanged
{
private int semesterScore;

public int SemesterScore
{
get { return semesterScore; }
set
{
semesterScore = value;
OnPropertyChanged();
}
}

public event PropertyChangedEventHandler PropertyChanged;

protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}

public class Semester : INotifyPropertyChanged
{
private int score;

public int Score
{
get { return score; }
set
{
score = value;
OnPropertyChanged();
}
}

public event PropertyChangedEventHandler PropertyChanged;

protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}

现在让我们将这些属性联系在一起,在您的 AttachProperties 中辅助方法。为此,我们将使 AttachProperties 方法采用 Expression<Func<T,object>。参数,这样我们就可以避免传递魔术字符串,并可以使用反射来检索属性名称。

顺便说一句,要在生产环境中运行它,您可能需要记住该反射代码以提高性能。

private static void AttachProperties<T1,T2>(Expression<Func<T1, object>> property1, T1 instance1, Expression<Func<T2, object>> property2,  T2 instance2)
where T1 : INotifyPropertyChanged
where T2 : INotifyPropertyChanged
{
var p1 = property1.GetPropertyInfo();
var p2 = property2.GetPropertyInfo();

//A NULL or empty PropertyName in PropertyChangeEventArgs means that all properties changed
//See: https://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.propertychanged(v=vs.110).aspx#Anchor_1

((INotifyPropertyChanged)instance1).PropertyChanged += (_, e) =>
{
if (e.PropertyName == p1.Name || string.IsNullOrEmpty(e.PropertyName))
{
SyncProperties(p1, p2, instance1, instance2);
}
};

((INotifyPropertyChanged)instance2).PropertyChanged += (_, e) =>
{
if (e.PropertyName == p2.Name || string.IsNullOrEmpty(e.PropertyName))
{
SyncProperties(p2, p1, instance2, instance1);
}
};
}

private static void SyncProperties(PropertyInfo sourceProperty, PropertyInfo targetProperty, object sourceInstance, object targetInstance)
{
var sourceValue = sourceProperty.GetValue(sourceInstance);
var targetValue = targetProperty.GetValue(targetInstance);

if (!sourceValue.Equals(targetValue))
{
targetProperty.SetValue(targetInstance, sourceValue);
}
}

最后,这是检索 PropertyInfo 的反射代码从参数:

public static class ReflectionExtension
{
public static PropertyInfo GetPropertyInfo<T>(this Expression<Func<T, object>> expression)
{
var memberExpression = GetMemberExpression(expression);
return (PropertyInfo)memberExpression.Member;
}

private static MemberExpression GetMemberExpression<TModel, T>(Expression<Func<TModel, T>> expression)
{
MemberExpression memberExpression = null;
if (expression.Body.NodeType == ExpressionType.Convert)
{
var body = (UnaryExpression)expression.Body;
memberExpression = body.Operand as MemberExpression;
}
else if (expression.Body.NodeType == ExpressionType.MemberAccess)
{
memberExpression = expression.Body as MemberExpression;
}

if (memberExpression == null)
{
throw new ArgumentException("Not a member access", "expression");
}

return memberExpression;
}
}

有了这一切,您现在可以保持两个属性同步:

public class PropertySyncTests
{
public void Should_sync_properties()
{
var semester = new Semester();
var student = new Student();
AttachProperties(x => x.Score, semester, x => x.SemesterScore, student);
semester.Score = 7;
student.SemesterScore.ShouldBe(7);
}
}

关于c# - 传递属性并上课,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38382410/

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