我正在开发一个用于修改游戏的工具,我想将自定义类型重新制作为 C# 类。该类型有 1 个必需属性和许多其他可选属性。我从 XML 文件中获取了自定义类的所有数据,那么有什么方法可以让这个构造函数变得更好呢?
public class Item(
string identifier,
string? nameIdentifier = null,
string? fallbackNameIdentifier = null,
string? descriptionIdentifier = null,
string? name = null,
string? aliases = null,
string? tags = null,
Item.Categories? category = null,
bool? allowAsExtraCargo = null,
float? interactDistance = null,
float? interactPriority = null,
bool? interactThroughWalls = null,
bool? hideConditionBar = null,
bool? hideConditionInTooltip = null,
bool? requireBodyInsideTrigger = null,
bool? requireCursorInsideTrigger = null,
bool? requireCampaignInteract = null,
bool? focusOnSelected = null,
float? offsetOnSelected = null,
float? health = null,
bool? allowSellWhenBroken = null,
bool? indestructible = null,
bool? damagedByExplosions = null,
float? explosionDamageMultiplier = null,
bool? damagedByProjectiles = null,
bool? damagedByMeleeWeapons = null,
bool? damagedByRepairTools = null,
bool? damagedByMonsters = null,
bool? fireProof = null,
bool? waterProof = null,
float? impactTolerance = null,
float? onDamagedThreshold = null,
float? sonarSize = null,
bool? useInHealthInterface = null,
bool? disableItemUsageWhenSelected = null,
string? cargoContainerIdentifier = null,
bool? useContainedSpriteColor = null,
bool? useContainedInventoryIconColor = null,
float? addedRepairSpeedMultiplier = null,
float? addedPickingSpeedMultiplier = null,
bool? cannotRepairFail = null,
string? equipConfirmationText = null,
bool? allowRotatingInEditor = null,
bool? showContentsInTooltip = null,
bool? canFlipX = null,
bool? canFlipY = null,
bool? isDangerous = null,
int? maxStackSize = null,
bool? allowDroppingOnSwap = null,
bool? resizeHorizontal = null,
bool? resizeVertical = null,
string? description = null,
string? allowedUpgrades = null,
bool? hideInMenus = null,
string? subcategory = null,
bool? linkable = null,
string? spriteColor = null,
float? scale = null)
{
public enum Categories
{
Decorative,
Machine,
Medical,
Weapon,
Diving,
Equipment,
Fuel,
Electrical,
Material,
Alien,
Wrecked,
Misc
}
// All of Item attributes
public required string identifier = identifier;
public string? nameIdentifier = nameIdentifier;
public string? fallbackNameIdentifier = fallbackNameIdentifier;
public string? descriptionIdentifier = descriptionIdentifier;
public string? name = name;
public string? aliases = aliases;
public string? tags = tags;
public Categories? category = category;
public bool? allowAsExtraCargo = allowAsExtraCargo;
public float? interactDistance = interactDistance;
public float? interactPriority = interactPriority;
public bool? interactThroughWalls = interactThroughWalls;
public bool? hideConditionBar = hideConditionBar;
public bool? hideConditionInTooltip = hideConditionInTooltip;
public bool? requireBodyInsideTrigger = requireBodyInsideTrigger;
public bool? requireCursorInsideTrigger = requireCursorInsideTrigger;
public bool? requireCampaignInteract = requireCampaignInteract;
public bool? focusOnSelected = focusOnSelected;
public float? offsetOnSelected = offsetOnSelected;
public float? health = health;
public bool? allowSellWhenBroken = allowSellWhenBroken;
public bool? indestructible = indestructible;
public bool? damagedByExplosions = damagedByExplosions;
public float? explosionDamageMultiplier = explosionDamageMultiplier;
public bool? damagedByProjectiles = damagedByProjectiles;
public bool? damagedByMeleeWeapons = damagedByMeleeWeapons;
public bool? damagedByRepairTools = damagedByRepairTools;
public bool? damagedByMonsters = damagedByMonsters;
public bool? fireProof = fireProof;
public bool? waterProof = waterProof;
public float? impactTolerance = impactTolerance;
public float? onDamagedThreshold = onDamagedThreshold;
public float? sonarSize = sonarSize;
public bool? useInHealthInterface = useInHealthInterface;
public bool? disableItemUsageWhenSelected = disableItemUsageWhenSelected;
public string? cargoContainerIdentifier = cargoContainerIdentifier;
public bool? useContainedSpriteColor = useContainedSpriteColor;
public bool? useContainedInventoryIconColor = useContainedInventoryIconColor;
public float? addedRepairSpeedMultiplier = addedRepairSpeedMultiplier;
public float? addedPickingSpeedMultiplier = addedPickingSpeedMultiplier;
public bool? cannotRepairFail = cannotRepairFail;
public string? equipConfirmationText = equipConfirmationText;
public bool? allowRotatingInEditor = allowRotatingInEditor;
public bool? showContentsInTooltip = showContentsInTooltip;
public bool? canFlipX = canFlipX;
public bool? canFlipY = canFlipY;
public bool? isDangerous = isDangerous;
public int? maxStackSize = maxStackSize;
public bool? allowDroppingOnSwap = allowDroppingOnSwap;
public bool? resizeHorizontal = resizeHorizontal;
public bool? resizeVertical = resizeVertical;
public string? description = description;
public string? allowedUpgrades = allowedUpgrades;
public bool? hideInMenus = hideInMenus;
public string? subcategory = subcategory;
public bool? linkable = linkable;
public string? spriteColor = spriteColor;
public float? scale = scale;
public List<XmlElement>? children = null;
/// <summary>
/// Gets this Item represent
/// </summary>
/// <returns>XmlElement represent of the Item</returns>
public XmlElement GetAsXml()
{
XmlDocument xmlDoc = new();
XmlElement newItem = xmlDoc.CreateElement("Item");
foreach(var attr in this.GetType().GetProperties())
{
if (attr.GetValue(this, null) != null)
{
newItem.SetAttribute(attr.Name.ToLower(), value: attr.GetValue(this, null).ToString());
}
}
newItem.InnerText = "";
if (this.children != null)
{
foreach (var child in this.children)
{
newItem.AppendChild(child);
}
}
return newItem;
}
}
}
我想到了两种可能的方法,将属性作为字典或包含所有可用属性的新自定义类型传递,但我不确定它们是否会更好。
如何定义一个类来保存所有可选属性:
public class ItemData
{
public string? nameIdentifier { get; set; } = null;
public string? fallbackNameIdentifier { get; set; } = null;
public string? descriptionIdentifier { get; set; } = null;
public string? name { get; set; } = null;
public string? aliases { get; set; } = null;
public string? tags { get; set; } = null;
public ItemData.Categories? category { get; set; } = null;
public bool? allowAsExtraCargo { get; set; } = null;
public float? interactDistance { get; set; } = null;
public float? interactPriority { get; set; } = null;
public bool? interactThroughWalls { get; set; } = null;
public bool? hideConditionBar { get; set; } = null;
public bool? hideConditionInTooltip { get; set; } = null;
public bool? requireBodyInsideTrigger { get; set; } = null;
public bool? requireCursorInsideTrigger { get; set; } = null;
public bool? requireCampaignInteract { get; set; } = null;
public bool? focusOnSelected { get; set; } = null;
public float? offsetOnSelected { get; set; } = null;
public float? health { get; set; } = null;
public bool? allowSellWhenBroken { get; set; } = null;
public bool? indestructible { get; set; } = null;
public bool? damagedByExplosions { get; set; } = null;
public float? explosionDamageMultiplier { get; set; } = null;
public bool? damagedByProjectiles { get; set; } = null;
public bool? damagedByMeleeWeapons { get; set; } = null;
public bool? damagedByRepairTools { get; set; } = null;
public bool? damagedByMonsters { get; set; } = null;
public bool? fireProof { get; set; } = null;
public bool? waterProof { get; set; } = null;
public float? impactTolerance { get; set; } = null;
public float? onDamagedThreshold { get; set; } = null;
public float? sonarSize { get; set; } = null;
public bool? useInHealthInterface { get; set; } = null;
public bool? disableItemUsageWhenSelected { get; set; } = null;
public string? cargoContainerIdentifier { get; set; } = null;
public bool? useContainedSpriteColor { get; set; } = null;
public bool? useContainedInventoryIconColor { get; set; } = null;
public float? addedRepairSpeedMultiplier { get; set; } = null;
public float? addedPickingSpeedMultiplier { get; set; } = null;
public bool? cannotRepairFail { get; set; } = null;
public string? equipConfirmationText { get; set; } = null;
public bool? allowRotatingInEditor { get; set; } = null;
public bool? showContentsInTooltip { get; set; } = null;
public bool? canFlipX { get; set; } = null;
public bool? canFlipY { get; set; } = null;
public bool? isDangerous { get; set; } = null;
public int? maxStackSize { get; set; } = null;
public bool? allowDroppingOnSwap { get; set; } = null;
public bool? resizeHorizontal { get; set; } = null;
public bool? resizeVertical { get; set; } = null;
public string? description { get; set; } = null;
public string? allowedUpgrades { get; set; } = null;
public bool? hideInMenus { get; set; } = null;
public string? subcategory { get; set; } = null;
public bool? linkable { get; set; } = null;
public string? spriteColor { get; set; } = null;
public float? scale { get; set; } = null;
public enum Categories
{
Decorative,
Machine,
Medical,
Weapon,
Diving,
Equipment,
Fuel,
Electrical,
Material,
Alien,
Wrecked,
Misc
}
}
并将
ItemData
类传递给 Item
构造函数:
public class Item
{
public string Identifier { get; private set; }
public ItemData OptionalData { get; private set; }
public List<XmlElement>? Children { get; private set; } = null;
public Item(string identifier, ItemData optionalData)
{
Identifier = identifier;
OptionalData = optionalData;
// Get the name
string? name = OptionalData.name;
}
}
您还必须更新您的
Item.GetAsXml()
方法才能正确序列化您的数据:
public XmlElement GetAsXml()
{
XmlDocument xmlDoc = new();
XmlElement newItem = xmlDoc.CreateElement("Item");
// Identifier
PropertyInfo identInfo = this.GetType().GetProperty(nameof(Identifier));
newItem.SetAttribute(identInfo.Name.ToLower(), value: identInfo.GetValue(this, null).ToString());
// Optional data attributes
foreach (var attr in OptionalData.GetType().GetProperties())
{
if (attr.GetValue(OptionalData, null) != null)
{
newItem.SetAttribute(attr.Name.ToLower(), value: attr.GetValue(OptionalData, null).ToString());
}
}
newItem.InnerText = "";
if (this.Children != null)
{
foreach (var child in this.Children)
{
newItem.AppendChild(child);
}
}
return newItem;
}
然后您可以创建如下项目:
ItemData itemData = new ItemData();
itemData.name = "name1";
itemData.health = 99;
Item item = new Item("identifier1", itemData);
当您序列化上述项目时:
XmlElement xml = item.GetAsXml();
Debug.WriteLine(xml.OuterXml);
您将得到以下输出:
<Item identifier="identifier1" name="name1" health="99"></Item>