我正在开发一个用于修改游戏的工具,我想将自定义类型重新制作为 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;
}
}
}
我想到过两种可能实现的方法,即将属性作为字典或包含所有可用属性的新自定义类型传递,但我不确定它们是否会更好。
那么,定义一个类来保存所有可选属性:
并将该类传递
ItemData
给Item
构造函数:您还必须更新您的
Item.GetAsXml()
方法以正确序列化您的数据:然后您可以创建如下项目:
当您序列化上述项目时:
您将获得以下输出: