Статическое значение поля только для чтения заменяетсяC#

Место общения программистов C#
Ответить Пред. темаСлед. тема
Anonymous
 Статическое значение поля только для чтения заменяется

Сообщение Anonymous »

.NET 8 Blazor
Статический класс, статические поля
Я не знаю, как назвать эту проблему, но у меня есть статическое чтение -only поле, в котором его значение отражается как другое значение в содержащем статическом классе. Я не уверен, что может вызвать это или где начать искать. Будем признательны за любую помощь.
Единственные формы, в которых это используется: =, ==, !=.
Например:

Код: Выделить всё

if (SOM == SystemOfMeasure.Imperial)
{
UOM = UnitOfMeasure.IN;
}
else
{
UOM = UnitOfMeasure.MM;
}
См. снимок экрана IntelliSense:
Изображение

Код: Выделить всё

public static class UnitOfMeasure
{
public static float MMToIn = (1 / 25.4F);
public static float inToMM = 25.4F;

public static readonly Unit Currency = new()
{
Name = "Currency",
DisplayName = "Currency",
Description = "Currency",
DefaultStep = .01F,
DefaultDecimalPlaces = CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalDigits,
DrawingPostFix = "",
ConversionFactor = 1
};

public static readonly Unit IN = new()
{
Name = "in",
DisplayName = "in",
Description = "inches",
DefaultStep = .001F,
DefaultDecimalPlaces = 4,
DrawingPostFix = "",
ConversionFactor = 1
};

public static readonly Unit MM = new()
{
Name = "mm",
DisplayName = "mm",
Description = "millimeter",
DefaultStep = .1F,
DefaultDecimalPlaces = 2,
DrawingPostFix = "",
ConversionFactor = MMToIn
};

public static readonly Unit SquareIN = new()
{
Name = "in²",
DisplayName = "in²",
Description = "inches squared",
DefaultStep = .001F,
DefaultDecimalPlaces = 5,
DrawingPostFix = "in²",
ConversionFactor = 1
};

public static readonly Unit SquareMM = new()
{
Name = "mm²",
DisplayName = "mm²",
Description = "inches squared",
DefaultStep = 1,
DefaultDecimalPlaces = 2,
DrawingPostFix = "mm²",
ConversionFactor = Pow(MMToIn, 2)
};

public static readonly Unit CubicIN = new()
{
Name = "in³",
DisplayName = "in³",
Description = "inches cubed",
DefaultStep = .001F,
DefaultDecimalPlaces = 5,
DrawingPostFix = "in³",
ConversionFactor = 1
};

public static readonly Unit CubicMM = new()
{
Name = "mm³",
DisplayName = "mm³",
Description = "inches cubed",
DefaultStep = 1,
DefaultDecimalPlaces = 2,
DrawingPostFix = "mm³",
ConversionFactor = Pow(MMToIn,  3)
};

public static readonly Unit L = new()
{
Name = "L",
DisplayName = "L",
Description = "Liters",
DefaultStep = .01F,
DefaultDecimalPlaces = 4,
DrawingPostFix = "L",
ConversionFactor = 1 / 0.01638706F
};

public static readonly Unit Gallon = new()
{
Name = "gal",
DisplayName = "gal",
Description = "Gallons",
DefaultStep = .01F,
DefaultDecimalPlaces = 4,
DrawingPostFix = "gal",
ConversionFactor = 1 / 0.004329F
};

public static readonly Unit ML = new()
{
Name = "ml",
DisplayName = "ml",
Description = "milliliters",
DefaultStep = .01F,
DefaultDecimalPlaces = 4,
DrawingPostFix = "ml",
ConversionFactor = 1 / 16.3871F
};

public static readonly Unit MG = new()
{
Name = "mg",
DisplayName = "mg",
Description = "milligrames",
DefaultStep = 1,
DefaultDecimalPlaces = 0,
DrawingPostFix = "mg",
ConversionFactor = 1
};

public static readonly Unit G = new()
{
Name = "g",
DisplayName = "g",
Description = "grames",
DefaultStep = .001F,
DefaultDecimalPlaces = 4,
DrawingPostFix = "g",
ConversionFactor = 1000
};

public static readonly Unit KG = new()
{
Name = "kg",
DisplayName = "kg",
Description = "kilogrames",
DefaultStep = .001F,
DefaultDecimalPlaces = 4,
DrawingPostFix = "kg",
ConversionFactor = 1000000
};

public static readonly Unit DEG = new()
{
Name = "deg",
DisplayName = "deg",
Description = "degrees",
DefaultStep = 1,
DefaultDecimalPlaces = 1,
DrawingPostFix = "˚",
ConversionFactor = PI / 180
};

public static readonly Unit Percent = new()
{
Name = "%",
DisplayName = "%",
Description = "percent",
DefaultStep = 1,
DefaultDecimalPlaces = 0,
DrawingPostFix = "",
ConversionFactor = 1
};

public static readonly Unit MGPerCubicIN = new()
{
Name = "mg/in³",
DisplayName = "mg/in³",
Description = "mg per inches cubed",
DefaultStep = .00001F,
DefaultDecimalPlaces = 0,
DrawingPostFix = "mg/in³",
ConversionFactor = 1
};

public static readonly Unit MGPerCubicMM = new()
{
Name = "mg/mm³",
DisplayName = "mg/mm³",
Description = "mg per inches cubed",
DefaultStep = .00001F,
DefaultDecimalPlaces = 2,
DrawingPostFix = "mg/mm³",
ConversionFactor = 1 / Pow(MMToIn, 3)
};

public static readonly Unit MGPerML = new()
{
Name = "mg/ml",
DisplayName = "mg/ml",
Description = "mg per milligrames",
DefaultStep = .00001F,
DefaultDecimalPlaces = 2,
DrawingPostFix = "mg/ml",
ConversionFactor = 1 * 16.3871F
};

public static readonly Unit GPerCubicIN = new()
{
Name = "g/in³",
DisplayName = "g/in³",
Description = "g per inches cubed",
DefaultStep = .00001F,
DefaultDecimalPlaces = 2,
DrawingPostFix = "g/in³",
ConversionFactor = 1000
};

public static readonly Unit GPerCubicMM = new()
{
Name = "g/mm³",
DisplayName = "g/mm³",
Description = "g per inches cubed",
DefaultStep = .00001F,
DefaultDecimalPlaces = 5,
DrawingPostFix = "g/mm³",
ConversionFactor = 1000 / Pow(MMToIn,  3 ) < b r   / >         } ; < b r   / > < b r   / >         p u b l i c   s t a t i c   r e a d o n l y   U n i t   G P e r M L   =   n e w ( ) < b r   / >         { < b r   / >                 N a m e   =   & q u o t ; g / m l & q u o t ; , < b r   / >                 D i s p l a y N a m e   =   & q u o t ; g / m l & q u o t ; , < b r   / >                 D e s c r i p t i o n   =   & q u o t ; g   p e r   m i l l i g r a m e s & q u o t ; , < b r   / >                 D e f a u l t S t e p   =   . 0 0 0 0 1 F , < b r   / >                 D e f a u l t D e c i m a l P l a c e s   =   4 , < b r   / >                 D r a w i n g P o s t F i x   =   & q u o t ; g / m l & q u o t ; , < b r   / >                 C o n v e r s i o n F a c t o r   =   1 0 0 0   *   1 6 . 3 8 7 1 F < b r   / >         } ; < b r   / > < b r   / >         p u b l i c   s t a t i c   r e a d o n l y   U n i t   L B S   =   n e w ( ) < b r   / >         { < b r   / >                 N a m e   =   & q u o t ; L B S & q u o t ; , < b r   / >                 D i s p l a y N a m e   =   & q u o t ; L B S & q u o t ; , < b r   / >                 D e s c r i p t i o n   =   & q u o t ; P o u n d s & q u o t ; , < b r   / >                 D e f a u l t S t e p   =   5 , < b r   / >                 D e f a u l t D e c i m a l P l a c e s   =   2 , < b r   / >                 D r a w i n g P o s t F i x   =   & q u o t ; & q u o t ; , < b r   / >                 C o n v e r s i o n F a c t o r   =   1 < b r   / >         } ; < b r   / > < b r   / >         p u b l i c   s t a t i c   r e a d o n l y   U n i t   T o n s   =   n e w ( ) < b r   / >         { < b r   / >                 N a m e   =   & q u o t ; t o n s & q u o t ; , < b r   / >                 D i s p l a y N a m e   =   & q u o t ; t o n s & q u o t ; , < b r   / >                 D e s c r i p t i o n   =   & q u o t ; T o n s & q u o t ; , < b r   / >                 D e f a u l t S t e p   =   . 1 F , < b r   / >                 D e f a u l t D e c i m a l P l a c e s   =   2 , < b r   / >                 D r a w i n g P o s t F i x   =   & q u o t ; & q u o t ; , < b r   / >                 C o n v e r s i o n F a c t o r   =   2 0 0 0 < b r   / >         } ; < b r   / > < b r   / >         p u b l i c   s t a t i c   r e a d o n l y   U n i t   k N   =   n e w ( ) < b r   / >         { < b r   / >                 N a m e   =   & q u o t ; k N & q u o t ; , < b r   / >                 D i s p l a y N a m e   =   & q u o t ; k N & q u o t ; , < b r   / >                 D e s c r i p t i o n   =   & q u o t ; k i l o n e w t o n & q u o t ; , < b r   / >                 D e f a u l t S t e p   =   . 1 F , < b r   / >                 D e f a u l t D e c i m a l P l a c e s   =   2 , < b r   / >                 D r a w i n g P o s t F i x   =   & q u o t ; & q u o t ; , < b r   / >                 C o n v e r s i o n F a c t o r   =   1   /   0 . 0 0 4 4 4 8 2 2 F < b r   / >         } ; < b r   / > < b r   / >         p u b l i c   s t a t i c   r e a d o n l y   U n i t   k P   =   n e w ( ) < b r   / >         { < b r   / >                 N a m e   =   & q u o t ; k P & q u o t ; , < b r   / >                 D i s p l a y N a m e   =   & q u o t ; k P & q u o t ; , < b r   / >                 D e s c r i p t i o n   =   & q u o t ; k i l o p o n d & q u o t ; , < b r   / >                 D e f a u l t S t e p   =   1 0 , < b r   / >                 D e f a u l t D e c i m a l P l a c e s   =   1 , < b r   / >                 D r a w i n g P o s t F i x   =   & q u o t ; & q u o t ; , < b r   / >                 C o n v e r s i o n F a c t o r   =   2 . 2 0 4 6 2 2 6 2 1 9 F < b r   / >         } ; < b r   / > < b r   / >         p u b l i c   s t a t i c   r e a d o n l y   U n i t   M P A   =   n e w ( ) < b r   / >         { < b r   / >                 N a m e   =   & q u o t ; M P a & q u o t ; , < b r   / >                 D i s p l a y N a m e   =   & q u o t ; M P a & q u o t ; , < b r   / >                 D e s c r i p t i o n   =   & q u o t ; M e g a p a s c a l & q u o t ; , < b r   / >                 D e f a u l t S t e p   =   1 0 , < b r   / >                 D e f a u l t D e c i m a l P l a c e s   =   2 , < b r   / >                 D r a w i n g P o s t F i x   =   & q u o t ; & q u o t ; , < b r   / >                 C o n v e r s i o n F a c t o r   =   1 4 5 < b r   / >         } ; < b r   / > < b r   / >         p u b l i c   s t a t i c   r e a d o n l y   U n i t   R P M   =   n e w ( ) < b r   / >         { < b r   / >                 N a m e   =   & q u o t ; R P M & q u o t ; , < b r   / >                 D i s p l a y N a m e   =   & q u o t ; R P M & q u o t ; , < b r   / >                 D e s c r i p t i o n   =   & q u o t ; R P M & q u o t ; , < b r   / >                 D e f a u l t S t e p   =   1 , < b r   / >                 D e f a u l t D e c i m a l P l a c e s   =   2 , < b r   / >                 D r a w i n g P o s t F i x   =   & q u o t ; & q u o t ; , < b r   / >                 C o n v e r s i o n F a c t o r   =   1 < b r   / >         } ; < b r   / > < b r   / >         p u b l i c   s t a t i c   r e a d o n l y   U n i t   H e r t z   =   n e w ( ) < b r   / >         { < b r   / >                 N a m e   =   & q u o t ; H z & q u o t ; , < b r   / >                 D i s p l a y N a m e   =   & q u o t ; H z & q u o t ; , < b r   / >                 D e s c r i p t i o n   =   & q u o t ; H z & q u o t ; , < b r   / >                 D e f a u l t S t e p   =   1 , < b r   / >                 D e f a u l t D e c i m a l P l a c e s   =   3 , < b r   / >          DrawingPostFix = "",
ConversionFactor = 60.0F
};

public static readonly Unit TPM = new()
{
Name = "TPM",
DisplayName = "Tablets Per Minute",
Description = "Tablets Per Minute",
DefaultStep = 1,
DefaultDecimalPlaces = 2,
DrawingPostFix = "",
ConversionFactor = 1
};

public static readonly Unit TPH = new()
{
Name = "TPH",
DisplayName = "Tablets Per Hour",
Description = "Tablets Per Hour",
DefaultStep = 1,
DefaultDecimalPlaces = 2,
DrawingPostFix = "",
ConversionFactor = 1
};

public static readonly Unit HR = new()
{
Name = "hr",
DisplayName = "hr",
Description = "hours",
DefaultStep = 1,
DefaultDecimalPlaces = 2,
DrawingPostFix = "",
ConversionFactor = 1
};

public static readonly Unit Tablets = new()
{
Name = "Tablets",
DisplayName = "Tablets",
Description = "Tablets",
DefaultStep = 1,
DefaultDecimalPlaces = 2,
DrawingPostFix = "",
ConversionFactor = 1,
ShowUOM = false
};

public static readonly Unit ToolStationQty = new()
{
Name = "ToolStationQty",
DisplayName = "Tool Station Qty",
Description = "Tool Station Qty",
DefaultStep = 1,
DefaultDecimalPlaces = 0,
DrawingPostFix = "",
ConversionFactor = 1
};

public static readonly Unit MainCompressionRollerQty = new()
{
Name = "MainCompressionRollerQty",
DisplayName = "Main Compression Rollers",
Description = "Main Compression Rollers",
DefaultStep = 1,
DefaultDecimalPlaces = 0,
DrawingPostFix = "",
ConversionFactor = 1
};

public static readonly Unit TipQty = new()
{
Name = "TipQty",
DisplayName = "Tip Qty",
Description = "Tip Qty",
DefaultStep = 1,
DefaultDecimalPlaces = 0,
DrawingPostFix = "",
ConversionFactor = 1
};

public static readonly Unit ThickToWidthRatio = new()
{
Name = "ThickToWidthRatio",
DisplayName = "Thickness To Width",
Description = "Thickness To Width",
DefaultStep = .01F,
DefaultDecimalPlaces = 2,
DrawingPostFix = "",
ConversionFactor = 1,
ShowUOM = false
};

public static readonly Unit CornerRadiusToWidth = new()
{
Name = "CornerRadiusToWidth",
DisplayName = "Corner Radius To Width",
Description = "Corner Radius To Width",
DefaultStep = .01F,
DefaultDecimalPlaces = 2,
DrawingPostFix = "",
ConversionFactor = 1,
ShowUOM = false
};

public static readonly Unit CupToThickRatio = new()
{
Name = "CupToThickRatio",
DisplayName = "Cup To Thickness",
Description = "Cup To Thickness",
DefaultStep = .01F,
DefaultDecimalPlaces = 2,
DrawingPostFix = "",
ConversionFactor = 1,
ShowUOM = false
};

public static readonly Unit LengthToWidthRatio = new()
{
Name = "LengthToWidthRatio",
DisplayName = "Length To Width",
Description = "Length To Width",
DefaultStep = .01F,
DefaultDecimalPlaces = 2,
DrawingPostFix = "",
ConversionFactor = 1,
ShowUOM = false
};

public static readonly Unit EndRadiusToWidthRatio = new()
{
Name = "EndRadiusToWidthRatio",
DisplayName = "End Radius To Width",
Description = "End Radius To Width",
DefaultStep = .01F,
DefaultDecimalPlaces = 2,
DrawingPostFix = "",
ConversionFactor = 1,
ShowUOM = false
};

public class Unit
{
public string Name { get; set; }
public string DisplayName { get; set; }
public string Description { get; set; }

public float DefaultStep { get; set; }
public int DefaultDecimalPlaces { get; set; }

public string DrawingPostFix { get; set; }

public float ConversionFactor { get; set; }

public bool ShowUOM { get; set; } = true;

public float Convert(Unit unitFrom, float value)
{
return (unitFrom.ConversionFactor / ConversionFactor) * value;
}

public static bool operator ==(Unit c1, Unit c2)
{
return c1?.Equals(c2) ?? false;
}

public static bool operator !=(Unit c1, Unit c2)
{
return !c1?.Equals(c2) ?? true;
}

public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType())
{
return false;
}

var objectToCompareWith = (Unit)obj;

return objectToCompareWith.Name == Name;
}

public override int GetHashCode()
{
return HashCode.Combine(Name);
}

}
}
Статические поля, доступные только для чтения, никогда не меняют значения

Подробнее здесь: https://stackoverflow.com/questions/787 ... s-replaced
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «C#»