如果我们创建一个类类型列表,它会反映列表中类的属性吗? [英] If we create a list of class type does that reflect properties of class in list?
本文介绍了如果我们创建一个类类型列表,它会反映列表中类的属性吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了一个类类型列表,即
a类,称为ADClient:
public class ADClient
{
public int InstanceID = 1 ;
public string ClientName = ;
public string HostIP = ;
public string UserName = ;
public string 密码= ;
public bool IsPasswordEncrypted = true ;
public string HostFilePath = ;
public string LocationTrim = ;
public string ImageVideoPath = ;
public string DestinationPath = ;
public string FileType = ;
public string RenameAfterCopy = ;
public string BinaryAscii = ;
public string LogFile = ;
public int NumberOfDaysToKeepLog = 0 ;
public int ADTransactionDays = 0 ;
public int 小时= 0 ;
public bool ADCleanUp = true ;
}
和ADClient类型列表
列表< adclient> ad_client = new 列表< adclient>();
现在,类已存在并在结束时声明list是在beginnning中创建的。
在某个位置,我必须从数据库传入的数据进行比较,无论类的属性是否与数据库中的数据的属性匹配。
那么,这段代码是否有用:
if (settingsOfAD.Equals(settingName))
// 其中settingName是传入名称来自数据库
或者我应该采用不同的方法吗?
解决方案
为类ADClient实现IComparer并编写自己的逻辑来比较ADClient的两个对象,如下所示
class ADClient:IComparer< adclient>
{
public int InstanceID = 1 跨度>;
public string ClientName = ;
public string HostIP = ;
public string UserName = ;
public string 密码= ;
public bool IsPasswordEncrypted = true ;
public string HostFilePath = ;
public string LocationTrim = ;
public string ImageVideoPath = ;
public string DestinationPath = ;
public string FileType = ;
public string RenameAfterCopy = ;
public string BinaryAscii = ;
public string LogFile = ;
public int NumberOfDaysToKeepLog = 0 ;
public int ADTransactionDays = 0 ;
public int 小时= 0 ;
public bool ADCleanUp = true ;
/// < 摘要 >
/// 如果x等于y,则返回0其他明智的1
/// < / summary >
/// < param name =x > < / PARAM >
/// < param name =y > < < span class =code-summarycomment> / param >
/ // < 返回 > < / returns >
public int 比较(ADClient x,ADC lient y)
{
int equals = 0 ;
if (x.InstanceID!= y.InstanceID)
{
返回 1 ;
}
if (x.ClientName!= y.ClientName)
{
返回 1 ;
}
if (x.UserName!= y.UserName)
{
返回 1 ;
}
如果(x.Password!= y.Password)
{
返回 1 ;
}
if (x.HostFilePath!= y.HostFilePath)
{
返回 1 ;
}
if (x.IsPasswordEncrypted!= y.IsPasswordEncrypted)
{
返回 1 ;
}
if (x.LocationTrim!= y.LocationTrim)
{
返回 1 ;
}
if (x.ImageVideoPath!= y.ImageVideoPath)
{
返回 1 ;
}
if (x.DestinationPath!= y.DestinationPath)
{
返回 1 ;
}
if (x.FileType!= y.FileType)
{
返回 1 ;
}
if (x.RenameAfterCopy!= y.RenameAfterCopy)
{
返回 1 ;
}
if (x.BinaryAscii!= y.BinaryAscii)
{
返回 1 ;
}
if (x.LogFile!= y.LogFile)
{
返回 1 ;
}
if (x.NumberOfDaysToKeepLog!= y.NumberOfDaysToKeepLog)
{
return 1 ;
}
if (x.ADTransactionDays!= y.ADTransactionDays)
{
return 1 ;
}
如果(x.Hours!= y.Hours)
{
return 1 ;
}
if (x.ADCleanUp!= y.ADCleanUp)
{
return 1 ;
}
return 等于;
}
} < / adclient > 跨度>
I created a list of class type i.e.
a class called ADClient :
public class ADClient
{
public int InstanceID = 1;
public string ClientName = "";
public string HostIP = "";
public string UserName = "";
public string Password = "";
public bool IsPasswordEncrypted = true;
public string HostFilePath = "";
public string LocationTrim = "";
public string ImageVideoPath = "";
public string DestinationPath = "";
public string FileType = "";
public string RenameAfterCopy = "";
public string BinaryAscii = "";
public string LogFile = "";
public int NumberOfDaysToKeepLog = 0;
public int ADTransactionDays = 0;
public int Hours = 0;
public bool ADCleanUp = true;
}
and a list of type ADClient
List<adclient> ad_client=new List<adclient>();
Now, class is existing and declared at the end while list is created in beginnning.
At a certain location I have to do a comparison from an incoming data from databases whether the properties of class matches that of the data in database.
So ,is this piece of code work:
if (settingsOfAD.Equals(settingName))
//where settingName is incoming name from database
Or should i have a different approach?
解决方案
Implement IComparer for class ADClient and write your own logic to compare two object of ADClient like shown below
class ADClient:IComparer<adclient> { public int InstanceID = 1; public string ClientName = ""; public string HostIP = ""; public string UserName = ""; public string Password = ""; public bool IsPasswordEncrypted = true; public string HostFilePath = ""; public string LocationTrim = ""; public string ImageVideoPath = ""; public string DestinationPath = ""; public string FileType = ""; public string RenameAfterCopy = ""; public string BinaryAscii = ""; public string LogFile = ""; public int NumberOfDaysToKeepLog = 0; public int ADTransactionDays = 0; public int Hours = 0; public bool ADCleanUp = true; /// <summary> /// Returns 0 if x equals to y other wise 1 /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <returns></returns> public int Compare(ADClient x, ADClient y) { int equals = 0; if (x.InstanceID != y.InstanceID ) { return 1; } if (x.ClientName != y.ClientName) { return 1; } if (x.UserName != y.UserName) { return 1; } if (x.Password != y.Password) { return 1; } if (x.HostFilePath != y.HostFilePath) { return 1; } if (x.IsPasswordEncrypted != y.IsPasswordEncrypted) { return 1; } if (x.LocationTrim != y.LocationTrim) { return 1; } if (x.ImageVideoPath != y.ImageVideoPath) { return 1; } if (x.DestinationPath != y.DestinationPath) { return 1; } if (x.FileType != y.FileType) { return 1; } if (x.RenameAfterCopy != y.RenameAfterCopy) { return 1; } if (x.BinaryAscii != y.BinaryAscii) { return 1; } if (x.LogFile != y.LogFile) { return 1; } if (x.NumberOfDaysToKeepLog != y.NumberOfDaysToKeepLog) { return 1; } if (x.ADTransactionDays != y.ADTransactionDays) { return 1; } if (x.Hours != y.Hours) { return 1; } if (x.ADCleanUp != y.ADCleanUp) { return 1; } return equals; } }</adclient>
这篇关于如果我们创建一个类类型列表,它会反映列表中类的属性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文