gpt4 book ai didi

c# - 使用泛型更新特定属性 (Winform C#)

转载 作者:行者123 更新时间:2023-11-30 23:29:20 25 4
gpt4 key购买 nike

抱歉,我是泛型的新手(我认为这是这种情况下所需要的)。

我想将 BindingList 变量传递给一个方法,该方法对定义的属性执行更新,然后返回通过/失败 (true/false) 值。这似乎比我想象的要难,所以需要一些帮助。这是工作代码和非工作代码。

首先,我有一个带有 BindingList 的类(有效):

public class BookMetaData : INotifyPropertyChanged
{
private string _bookMetaDataTitle;
[DescriptionLocalized(typeof(ResourcesClassBooks), "BookMetaDataTitleComment")]
[DisplayNameLocalized(typeof(ResourcesClassBooks), "BookMetaDataTitleDisplayName")]
public string BookMetaDataTitle { get { return _bookMetaDataTitle; } set { SetField(ref _bookMetaDataTitle, value, "BookMetaDataTitle"); } }

#region handle property changes
public event PropertyChangedEventHandler PropertyChanged;
protected bool SetField<T>(ref T field, T value, string propertyName)
{
//if the value did not change, do nothing.
if (EqualityComparer<T>.Default.Equals(field, value)) return false;
//the value did change, so make the modification.
field = value;
return true;
}
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}

接下来,我创建一个代表绑定(bind)列表的变量(有效):

BindingList<BookMetaData> bookMetaData = new BindingList<BookMetaData>(); //metadata for the books

准备发送类变量和特定属性以更新到泛型方法。请注意,这在技术上可行,但其余代码无效:

private bool Test()
{
//the code below would probably be in a foreach loop and would not pass a single index.
ProcessBookTitle(bookMetaData, 0, typeof(BookMetaData).GetProperty(nameof(BookMetaData.BookMetaDataCoverage)));
return true;
}

我想要这个泛型方法(它叫方法吗?)来更新 BindingList 类变量的指定属性。如果一般业务逻辑出现问题,则返回 false。否则,返回 true(无效):

private void ProcessBookTitle<T>(BindingList<T> bindingList, int classIndex, PropertyInfo prop)
{
try
{
//do some things...
//If I manually wrote the next line, it would look like this:
//bookMetaData[0].BookMetaDataTitle = "My Book Title";
bindingList[classIndex].prop = "My Book Title"; //error = 'T' does not contain the information for 'prop'...
//maybe do some other things...
return true; //cannot find a way to return bool.
}
catch
{
//something went wrong somewhere, so return false.
return false; //does not work because cannot return a bool.
}
}

有没有一种方法可以将类变量的名称和要更新的属性发送到另一个方法?此外,是否可以在返回 bool(和/或返回成功或失败的其他方式)的方法中执行此操作?

在此先感谢您提供的任何帮助。

编辑:返回 bool 值的要求是确保传递方法知道整个过程是否成功。更新代码以显示这一点。

最佳答案

您可以像@Yacoub Massad 提到的那样使用反射。如果你想知道它是怎么回事,那就是这样的。我们在这里所做的是获取泛型类型 T 的类型并访问其属性信息。现在既然你提到了从文件中读取、解析信息等,我建议你看看 Delegate Actions With Return Values .但它本身是另一个故事。

    static private bool ProcessBookTitle<T>(BindingList<T> bindingList, int classIndex, string propertyName) 
where T : class
{
try
{
Type type = typeof(T);
PropertyInfo propertyInfo = type.GetProperty(propertyName);

object val = "Do some processing whatever here.";

propertyInfo.SetValue(bindingList[classIndex], Convert.ChangeType(val, propertyInfo.PropertyType), null);

return true;
}
catch (Exception ex)
{
// do something with the exception

return false;
}
}

关于c# - 使用泛型更新特定属性 (Winform C#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35517782/

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