Я разрабатываю инструмент для модификации игры и хочу переделать пользовательский тип в класс C#. Этот тип имеет 1 обязательное свойство и множество других необязательных. Я получаю все данные для пользовательского класса из XML-файла. М о г у л и я к а к - н и б у д ь у л у ч ш и т ь э т о т к о н с т р у к т о р ? < / p > < b r / > < p r e c l a s s = " l a n g - c s P r e t t y p r i n t - o v e r r i d e " > < c o d e > p u b l i c c l a s s I t e m ( < b r / > s t r i n g i d e n t i f i e r , < b r / > s t r i n g ? n a m e I d e n t i f i e r = n u l l , < b r / > s t r i n g ? f a l l b a c k N a m e I d e n t i f i e r = n u l l , < b r / > s t r i n g ? d e s c r i p t i o n I d e n t i f i e r = n u l l , < b r / > s t r i n g ? n a m e = n u l l , < b r / > s t r i n g ? a l i a s e s = n u l l , < b r / > s t r i n g ? t a g s = n u l l , < b r / > I t e m . C a t e g o r i e s ? c a t e g o r y = n u l l , < b r / > b o o l ? a l l o w A s E x t r a C a r g o = n u l l , < b r / > f l o a t ? i n t e r a c t D i s t a n c e = n u l l , < b r / > f l o a t ? i n t e r a c t P r i o r i t y = n u l l , < b r / > b o o l ? i n t e r a c t T h r o u g h W a l l s = n u l l , < b r / > b o o l ? h i d e C o n d i t i o n B a r = n u l l , < b r / > b o o l ? h i d e C o n d i t i o n I n T o o l t i p = n u l l , < b r / > b o o l ? r e q u i r e B o d y I n s i d e T r i g g e r = n u l l , < b r / > b o o l ? r e q u i r e C u r s o r I n s i d e T r i g g e r = n u l l , < b r / > b o o l ? r e q u i r e C a m p a i g n I n t e r a c t = n u l l , < b r / > b o o l ? f o c u s O n S e l e c t e d = n u l l , < b r / > f l o a t ? o f f s e t O n S e l e c t e d = n u l l , < b r / > f l o a t ? h e a l t h = n u l l , < b r / > b o o l ? a l l o w S e l l W h e n B r o k e n = n u l l , < b r / > b o o l ? i n d e s t r u c t i b l e = n u l l , < b r / > b o o l ? d a m a g e d B y E x p l o s i o n s = n u l l , < b r / > f l o a t ? e x p l o s i o n D a m a g e M u l t i p l i e r = n u l l , < b r / > b o o l ? d a m a g e d B y P r o j e c t i l e s = n u l l , < b r / > b o o l ? d a m a g e d B y M e l e e W e a p o n s = n u l l , < b r / > b o o l ? d a m a g e d B y R e p a i r T o o l s = n u l l , < b r / > b o o l ? d a m a g e d B y M o n s t e r s = n u l l , < b r / > b o o l ? f i r e P r o o f = n u l l , < b r / > b o o l ? w a t e r P r o o f = n u l l , < b r / > f l o a t ? i m p a c t T o l e r a n c e = n u l l , < b r / > f l o a t ? o n D a m a g e d T h r e s h o l d = n u l l , < b r / > f l o a t ? s o n a r S i z e = n u l l , < b r / > b o o l ? u s e I n H e a l t h I n t e r f a c e = n u l l , < b r / > b o o l ? d i s a b l e I t e m U s a g e W h e n S e l e c t e d = n u l l , < b r / > s t r i n g ? c a r g o C o n t a i n e r I d e n t i f i e r = n u l l , < b r / > b o o l ? u s e C o n t a i n e d S p r i t e C o l o r = n u l l , < b r / > b o o l ? u s e C o n t a i n e d I n v e n t o r y I c o n C o l o r = n u l l , < b r / > f l o a t ? a d d e d R e p a i r S p e e d M u l t i p l i e r = n u l l , < b r / > f l o a t ? a d d e d P i c k i n g S p e e d M u l t i p l i e r = n u l l , < b r / > b o o l ? c a n n o t R e p a i r F a i l = n u l l , < b r / > s t r i n g ? e q u i p C o n f i r m a t i o n T e x t = n u l l , < b r / > b o o l ? a l l o w R o t a t i n g I n E d i t o r = n u l l , < b r / > b o o l ? s h o w C o n t e n t s I n T o o l t i p = n u l l , < b r / > b o o l ? c a n F l i p X = n u l l , < b r / > b o o l ? c a n F l i p Y = n u l l , < b r / > b o o l ? i s D a n g e r o u s = n u l l , < b r / > i n t ? m a x S t a c k S i z e = n u l l , < b r / > b o o l ? a l l o w D r o p p i n g O n S w a p = n u l l , < b r / > b o o l ? r e s i z e H o r i z o n t a l = n u l l , < b r / > b o o l ? r e s i z e V e r t i c a l = n u l l , < b r / > s t r i n g ? d e s c r i p t i o n = n u l l , < b r / > s t r i n g ? a l l o w e d U p g r a d e s = n u l l , < b r / > b o o l ? h i deInMenus = 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? children = null;
///
/// Gets this Item represent
///
/// XmlElement represent of the Item
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;
}
}
}
Я подумал о двух способах, которыми это потенциально можно сделать: передать свойства либо в виде словаря, либо в виде нового пользовательского типа, содержащего все доступные свойства, но я не уверен, что они будут лучше.
Подробнее здесь: https://stackoverflow.com/questions/790 ... optional-h
Мой пользовательский тип имеет множество различных свойств, большинство из которых являются необязательными. Как я могу ⇐ C#
Место общения программистов C#
1727176595
Anonymous
Я разрабатываю инструмент для модификации игры и хочу переделать пользовательский тип в класс C#. Этот тип имеет 1 обязательное свойство и множество других необязательных. Я получаю все данные для пользовательского класса из XML-файла. М о г у л и я к а к - н и б у д ь у л у ч ш и т ь э т о т к о н с т р у к т о р ? < / p > < b r / > < p r e c l a s s = " l a n g - c s P r e t t y p r i n t - o v e r r i d e " > < c o d e > p u b l i c c l a s s I t e m ( < b r / > s t r i n g i d e n t i f i e r , < b r / > s t r i n g ? n a m e I d e n t i f i e r = n u l l , < b r / > s t r i n g ? f a l l b a c k N a m e I d e n t i f i e r = n u l l , < b r / > s t r i n g ? d e s c r i p t i o n I d e n t i f i e r = n u l l , < b r / > s t r i n g ? n a m e = n u l l , < b r / > s t r i n g ? a l i a s e s = n u l l , < b r / > s t r i n g ? t a g s = n u l l , < b r / > I t e m . C a t e g o r i e s ? c a t e g o r y = n u l l , < b r / > b o o l ? a l l o w A s E x t r a C a r g o = n u l l , < b r / > f l o a t ? i n t e r a c t D i s t a n c e = n u l l , < b r / > f l o a t ? i n t e r a c t P r i o r i t y = n u l l , < b r / > b o o l ? i n t e r a c t T h r o u g h W a l l s = n u l l , < b r / > b o o l ? h i d e C o n d i t i o n B a r = n u l l , < b r / > b o o l ? h i d e C o n d i t i o n I n T o o l t i p = n u l l , < b r / > b o o l ? r e q u i r e B o d y I n s i d e T r i g g e r = n u l l , < b r / > b o o l ? r e q u i r e C u r s o r I n s i d e T r i g g e r = n u l l , < b r / > b o o l ? r e q u i r e C a m p a i g n I n t e r a c t = n u l l , < b r / > b o o l ? f o c u s O n S e l e c t e d = n u l l , < b r / > f l o a t ? o f f s e t O n S e l e c t e d = n u l l , < b r / > f l o a t ? h e a l t h = n u l l , < b r / > b o o l ? a l l o w S e l l W h e n B r o k e n = n u l l , < b r / > b o o l ? i n d e s t r u c t i b l e = n u l l , < b r / > b o o l ? d a m a g e d B y E x p l o s i o n s = n u l l , < b r / > f l o a t ? e x p l o s i o n D a m a g e M u l t i p l i e r = n u l l , < b r / > b o o l ? d a m a g e d B y P r o j e c t i l e s = n u l l , < b r / > b o o l ? d a m a g e d B y M e l e e W e a p o n s = n u l l , < b r / > b o o l ? d a m a g e d B y R e p a i r T o o l s = n u l l , < b r / > b o o l ? d a m a g e d B y M o n s t e r s = n u l l , < b r / > b o o l ? f i r e P r o o f = n u l l , < b r / > b o o l ? w a t e r P r o o f = n u l l , < b r / > f l o a t ? i m p a c t T o l e r a n c e = n u l l , < b r / > f l o a t ? o n D a m a g e d T h r e s h o l d = n u l l , < b r / > f l o a t ? s o n a r S i z e = n u l l , < b r / > b o o l ? u s e I n H e a l t h I n t e r f a c e = n u l l , < b r / > b o o l ? d i s a b l e I t e m U s a g e W h e n S e l e c t e d = n u l l , < b r / > s t r i n g ? c a r g o C o n t a i n e r I d e n t i f i e r = n u l l , < b r / > b o o l ? u s e C o n t a i n e d S p r i t e C o l o r = n u l l , < b r / > b o o l ? u s e C o n t a i n e d I n v e n t o r y I c o n C o l o r = n u l l , < b r / > f l o a t ? a d d e d R e p a i r S p e e d M u l t i p l i e r = n u l l , < b r / > f l o a t ? a d d e d P i c k i n g S p e e d M u l t i p l i e r = n u l l , < b r / > b o o l ? c a n n o t R e p a i r F a i l = n u l l , < b r / > s t r i n g ? e q u i p C o n f i r m a t i o n T e x t = n u l l , < b r / > b o o l ? a l l o w R o t a t i n g I n E d i t o r = n u l l , < b r / > b o o l ? s h o w C o n t e n t s I n T o o l t i p = n u l l , < b r / > b o o l ? c a n F l i p X = n u l l , < b r / > b o o l ? c a n F l i p Y = n u l l , < b r / > b o o l ? i s D a n g e r o u s = n u l l , < b r / > i n t ? m a x S t a c k S i z e = n u l l , < b r / > b o o l ? a l l o w D r o p p i n g O n S w a p = n u l l , < b r / > b o o l ? r e s i z e H o r i z o n t a l = n u l l , < b r / > b o o l ? r e s i z e V e r t i c a l = n u l l , < b r / > s t r i n g ? d e s c r i p t i o n = n u l l , < b r / > s t r i n g ? a l l o w e d U p g r a d e s = n u l l , < b r / > b o o l ? h i deInMenus = 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? children = null;
///
/// Gets this Item represent
///
/// XmlElement represent of the Item
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;
}
}
}
Я подумал о двух способах, которыми это потенциально можно сделать: передать свойства либо в виде словаря, либо в виде нового пользовательского типа, содержащего все доступные свойства, но я не уверен, что они будут лучше.
Подробнее здесь: [url]https://stackoverflow.com/questions/79017984/my-custom-type-has-many-different-properties-with-most-of-them-being-optional-h[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия