C # использует интерфейсы для установки общих свойств и методов.
Используйте интерфейсы, которые определяют общие элементы обоих классов.
public Interface UserInfo
{
string AS {get; set;}
string BS {get; set;}
}
Вы все еще можете использовать базовый класс, он должен быть первым в списке, т.е. после ":".
public class UserData: UserInfo
public class User: UserInfo
Поскольку ОБА классы уже реализуют Интерфейс, нет никаких изменений, кроме как их вывод из интерфейса.
Редактировать
Поскольку класс UserData
не может быть изменен (по какой-либо причине, определен извне или публично доступен через и API
) и не является sealed
, из него можно извлечь класс и добавить интерфейс:
public class UserData1: UserData, UserInfo
{
// since the base class already implements the public properties as defined
// in the interface, no implementation is required here
// however any defined constructors in the base class must be present here:
// repeat per constructor
public UserData1() : base() // add parameters: UserData1(int i):base(i)
{
// this can be left empty
}
}
Полностью вымышленный вариант использования:
Предполагает:
BusinessLogic.UserData
передается методу как:
List<BusinessLogic.UserData> userData
. и одно значение BusinessLogic.UserData для полноты.
Массив уровня класса, уже созданный и заполненный, доступен как public User[] users
.
Для этого также требуется `использование System.Linq;" для массового преобразования типов.
public void ProcessAll(List<BusinessLogic.UserData> userData,BusinessLogic.UserData single)
{
List<UserInfo> AllData = new List<UserInfo>();
AllData.AddRange(userData.ConvertAll(new Converter<BusinessLogic.UserData, UserInfo>(i => i as UserData1)));
AllData.AddRange(users);
// cast the single and add it to the list
AllData.Add((UserInfo)((UserData1)single));// note the extra cast
foreach(var user in AllData)
{
//note CS is not available from AllData since it is not defined in the interface
// not the most elegant code, but proves the point
Console.WriteLine("AS=" + user.AS + " BS=" + user.BS);
}
//Let us replace the first element in userData with itself from AllData does nothing, but shows how to do this.
if(AllData[0] is BusinessLogic.UserData)
//since add order is maintained in a list this if is not needed.
userData[0] = (BusinessLogic.UserData)AllData[0];
// since BusinessLogic.UserData is a reference type(class not struct) we can modify CS, but only if it is cast back to BusinessLogic.UserData first
if(AllData[0] is BusinessLogic.UserData)
((BusinessLogic.UserData)AllData[0]).CS="Hello World";
}