У меня есть система, где каждый интерфейс/класс имеет два кода (InterfaceCode, subtypecode ) - это для эффективного сохранения и повторного генерирования объектов из шестигранников. T.subtypecode (см. Ниже). К сожалению, это не сработает, потому что я не могу получить доступ к членам параметра типа (CS0704).
public static T AsType(string ItemCode) where T : Iitem
{
if (ItemCode.Length < 5) return null;
ItemCodeDes itemCode = new ItemCodeDes(ItemCode);
if(itemCode.InterfaceCode != T.InterfaceCode) return null; //Error CS0704 Cannot do non-virtual member lookup in 'T' because it is a type parameter
if(itemCode.SubtypeCode != T.SubtypeCode) return null; //same here...
//...
}
< /code>
с < /p>
public static ushort SubtypeCode { get; } = 0;
public static ushort InterfaceCode { get; } = 0;
< /code>
Я действительно не хочу, чтобы таблицы поиска жестких кодов для каждого класса (будет около 100-200). < /p>
Есть ли хорошее и чистое решение моей проблемы (без использования отражения)?public class Iitem
{
public static ushort SubtypeCode { get; } = 0;
public static ushort InterfaceCode { get; } = 0;
public Iitem(string itemName, string description, ItemTier tier, ushort id)
{
Name = itemName;
Tier = tier;
Description = description;
Id = id;
}
public string Name { get; }
public string Description { get; }
public ItemTier Tier { get; }
public ushort Id { get; }
///
/// Gibt den itemspezifischen Code zurück.
///
/// Itemcode als Hex-String
public virtual string GetItemCode()
{
return GenerateItemCode(0, 0);
}
internal string GenerateItemCode(ushort Interface, ushort Subtype, ulong Generationspecifics = 0)
{
return Interface.ToString("X1") + Subtype.ToString("X1") + Id.ToString("X3") + (Generationspecifics != 0 ? Generationspecifics.ToString("X") : "");
}
///
/// Generiert einen String mit den Stats und einer Beschreibung des Items.
///
///
public virtual string PrintItem()
{
return Name + " (" + Tier.ToString() + ") - " + Description;
}
public enum ItemTier
{
Common,
Uncommon,
Epic,
Legendary,
Heavengrade,
Forbidden,
}
}
private class ItemCodeDes
{
public ItemCodeDes(string ItemCode)
{
if (ItemCode.Length < 5) return;
InterfaceCode = ushort.Parse(ItemCode[0].ToString(), System.Globalization.NumberStyles.HexNumber);
SubtypeCode = ushort.Parse(ItemCode[1].ToString(), System.Globalization.NumberStyles.HexNumber);
ItemId = ushort.Parse(ItemCode.Substring(2, 3), System.Globalization.NumberStyles.HexNumber);
if (ItemCode.Length > 5)
Generationspecifics = ItemCode.Substring(5);
}
public ushort InterfaceCode { get; }
public ushort SubtypeCode { get; }
public ushort ItemId { get; }
public string Generationspecifics { get; } = "";
}
Подробнее здесь: https://stackoverflow.com/questions/762 ... because-it