当前位置:  开发笔记 > 编程语言 > 正文

如何在.NET中创建自己的原始数据类型?

如何解决《如何在.NET中创建自己的原始数据类型?》经验,为你挑选了2个好方法。

如何创建自己的原语?例如,范围为1-10的整数.

编辑:这来自Rosetta Code的任务.

定义基元数据类型:演示如何定义行为类似于整数但最低有效值为1且最高有效值为10的类型.

我在这里添加它是因为我认为它可能对其他人有用.



1> Michael Stum..:

好吧,让我们来看看.首先,CLR中有一些数据类型.那些不能被修改或添加新的,因为它们是标准的一部分.您可以在此处或此处找到列表.那是C#,但是这个列表也应该存在于某个地方的VB.net中,它应该看起来相同,因为底层的CLR是相同的.此外,列表不完整,因为浮动和字符丢失,但你明白了.

但是,有一些结构封装了这些数据类型并添加了一些额外的功能.例如,System.Int32只是一个简单的标准结构,不涉及任何魔法.随意在Reflector中查看它,它在mscorlib中:

[Serializable, StructLayout(LayoutKind.Sequential), ComVisible(true)]
public struct Int32 : IComparable, IFormattable, IConvertible, IComparable, IEquatable

所以你想要自己的"1到10"整数?然后我建议查看最近的合适类型,即Int16或者Byte.如果你看一下它们,你会发现它们看起来有些相似,但是它们基于一种内置数据类型.

只是复制/粘贴和修改一些内置结构(即System.Byte)并不完全有效,因为一些成员是内部的(即NumberFormatInfo.ValidateParseStyleInteger),但Reflector可以在这里提供帮助.



2> Jonathan All..:
Structure LimitedInt
  Implements IComparable(Of LimitedInt)
  Implements IEquatable(Of LimitedInt)

  Private m_Value As Integer 'treat the default, 0 as being really 1'

  Public ReadOnly Property Value() As Integer
   Get
     Return If(m_Value = 0, 1, m_Value)
   End Get
  End Property

  Public Sub New(ByVal value As Integer)
   If value < 1 Or value > 10 Then 
      Throw New ArgumentOutOfRangeException("value")
   End If
   m_Value = value
  End Sub

  Public Function CompareTo(ByVal other As LimitedInt) As Integer _
      Implements System.IComparable(Of LimitedInt).CompareTo
   Return Me.Value - other.Value
  End Function

  Public Overloads Function Equals(ByVal other As LimitedInt) As Boolean _
     Implements System.IEquatable(Of LimitedInt).Equals
   Return Me.Value = other.Value
  End Function

  Public Overrides Function GetHashCode() As Integer
   Return Value.GetHashCode
  End Function

  Public Overrides Function Equals(ByVal obj As Object) As Boolean
   If TypeOf obj Is LimitedInt Then Return CType(obj, LimitedInt) = Me
  End Function

  Public Shared Operator =(ByVal left As LimitedInt, _
     ByVal right As LimitedInt) As Boolean
   Return left.Equals(right)
  End Operator

  Public Shared Operator <>(ByVal left As LimitedInt, _
     ByVal right As LimitedInt) As Boolean
   Return Not (left = right)
  End Operator

  Public Shared Operator +(ByVal left As LimitedInt, _
     ByVal right As LimitedInt) As LimitedInt
   Dim temp As Integer = left.Value + right.Value
   Select Case temp
     Case 1 To 10 : Return New LimitedInt(temp)
     Case Else : Throw New OverflowException
   End Select
  End Operator

  Public Shared Operator -(ByVal left As LimitedInt, _
     ByVal right As LimitedInt) As LimitedInt
   Dim temp As Integer = left.Value - right.Value
   Select Case temp
     Case 1 To 10 : Return New LimitedInt(temp)
     Case Else : Throw New OverflowException
   End Select
  End Operator

  Public Shared Operator *(ByVal left As LimitedInt, _
     ByVal right As LimitedInt) As LimitedInt
   Dim temp As Integer = left.Value * right.Value
   Select Case temp
     Case 1 To 10 : Return New LimitedInt(temp)
     Case Else : Throw New OverflowException
   End Select
  End Operator

  Public Shared Operator /(ByVal left As LimitedInt, _
     ByVal right As LimitedInt) As Double
   Return left.Value / right.Value
  End Operator

  Public Shared Operator \(ByVal left As LimitedInt, _
     ByVal right As LimitedInt) As LimitedInt
   Dim temp As Integer = left.Value \ right.Value
   Select Case temp
     Case 1 To 10 : Return New LimitedInt(temp)
     Case Else : Throw New OverflowException
   End Select
  End Operator

  Public Shared Operator Mod(ByVal left As LimitedInt, _
     ByVal right As LimitedInt) As LimitedInt
   Dim temp As Integer = left.Value Mod right.Value
   Select Case temp
     Case 1 To 10 : Return New LimitedInt(temp)
     Case Else : Throw New OverflowException
   End Select
  End Operator

  Public Shared Operator And(ByVal left As LimitedInt, _
     ByVal right As LimitedInt) As LimitedInt
   Dim temp As Integer = left.Value And right.Value
   Select Case temp
     Case 1 To 10 : Return New LimitedInt(temp)
     Case Else : Throw New OverflowException
   End Select
  End Operator

  Public Shared Operator Or(ByVal left As LimitedInt, _
     ByVal right As LimitedInt) As LimitedInt
   Dim temp As Integer = left.Value Or right.Value
   Select Case temp
     Case 1 To 10 : Return New LimitedInt(temp)
     Case Else : Throw New OverflowException
   End Select
  End Operator

  Public Shared Operator Xor(ByVal left As LimitedInt, _
     ByVal right As LimitedInt) As LimitedInt
   Dim temp As Integer = left.Value Xor right.Value
   Select Case temp
     Case 1 To 10 : Return New LimitedInt(temp)
     Case Else : Throw New OverflowException
   End Select
  End Operator

  Public Shared Operator ^(ByVal left As LimitedInt, _
     ByVal right As LimitedInt) As Double
   Return left.Value ^ right.Value
  End Operator

  Public Shared Operator <(ByVal left As LimitedInt, _
     ByVal right As LimitedInt) As Boolean
   Return left.Value < right.Value
  End Operator

  Public Shared Operator >(ByVal left As LimitedInt, _
     ByVal right As LimitedInt) As Boolean
   Return left.Value > right.Value
  End Operator

  Public Shared Operator <=(ByVal left As LimitedInt, _
     ByVal right As LimitedInt) As Boolean
   Return left.Value <= right.Value
  End Operator

  Public Shared Operator >=(ByVal left As LimitedInt, _
     ByVal right As LimitedInt) As Boolean
   Return left.Value >= right.Value
  End Operator

  Public Shared Widening Operator CType(ByVal left As LimitedInt) As Integer
   Return left.Value
  End Operator

  Public Shared Narrowing Operator CType(ByVal left As Integer) As LimitedInt
   Return New LimitedInt(left)
  End Operator


End Structure


不太确定为什么要拒​​绝这样做。这很丑是SO的错,而VB太冗长是设计决定。总的来说,我认为此代码段非常有用,至少可以澄清一下:数据类型不是神奇的构造,它们是普通的类/结构。
推荐阅读
惬听风吟jyy_802
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有