C#中的typedef [英] typedef in C#

查看:165
本文介绍了C#中的typedef的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在C中有typedef但是如果我在C中查看这个例子,那么c#

怎么样?

We have typedef in C but what about c#
if I look at this example in C

typedef struct record {
  int value;
} record;





我写了这段代码



I wrote this code

public struct record
{
    public int value;
    public record(int v)
    {
        value = v;
    }
}





1-在C代码中用typedef表示相同的东西是正确的/>
2-它是将c代码中的typedef传递给C#的唯一方法。如果有另一种方式请告诉我

谢谢



1- is it correct to represent the same thing by typedef in C code
2- is it the only way to transfer typedef in the c code to C#.if there is another way please tell me
thanks

推荐答案

根据你需要的范围,有两种方式。



如果你只需要一个.cs文件,那么你可以使用使用指令实现此目的。

There are two ways depending on the scope you need.

If you only need this within one .cs file then you can use the using directive to achieve this.
using record = System.Int32;





如果您需要跨多个文件然后作为包装器的简单结构将完成这项工作。



If you need it across multiple files then a simple struct as a wrapper will do the job.

/// <summary>
/// A wrapper around <see cref="System.Int32"/>.
/// </summary>
public struct Record
{
    // A struct's fields should not be exposed
    private int value;

    // As we are using implicit conversions we can keep the constructor private
    private Record(int value)
    {
        this.value = value;
    }

    /// <summary>
    /// Implicitly converts a <see cref="System.Int32"/> to a Record.
    /// </summary>
    /// <param name="value">The <see cref="System.Int32"/> to convert.</param>
    /// <returns>A new Record with the specified value.</returns>
    public static implicit operator Record(int value)
    {
        return new Record(value);
    }
    /// <summary>
    /// Implicitly converts a Record to a <see cref="System.Int32"/>.
    /// </summary>
    /// <param name="record">The Record to convert.</param>
    /// <returns>
    /// A <see cref="System.Int32"/> that is the specified Record's value.
    /// </returns>
    public static implicit operator int(Record record)
    {
        return record.value;
    }
}



由于Record被隐式转换为/从int,我们获得 System.Int32的所有功能免费!


typedef class ...



typedef class ...

#region TypeDefine
    public class typedef
    {
        #region Variables
        protected T m_value;
        #endregion

        #region Constructor/Destructor
        public typedef()
        {
            this.m_value = default(T);
        }

        public typedef(T value)
        {
            this.m_value = value;
        }
        #endregion

        #region Override Methods
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return false;
            }
            if (!object.ReferenceEquals(this.m_value.GetType(), obj.GetType()))
            {
                return false;
            }
            return this.m_value.Equals(obj);
        }

        public override int GetHashCode()
        {
            return this.m_value.GetHashCode();
        }

        public override string ToString()
        {
            return this.m_value.ToString();
        }
        #endregion

        #region Static Methods

        private static bool IsNumeric(object value)
        {
            return ((value is sbyte) || (value is byte) || (value is short) || (value is ushort) || (value is int) || (value is uint) || (value is long) || (value is ulong) || (value is float) || (value is double) || (value is decimal));
        }

        private static bool IsInteger(object value)
        {
            return ((value is sbyte) || (value is byte) || (value is short) || (value is ushort) || (value is int) || (value is uint) || (value is long) || (value is ulong));
        }

        private static bool IsFloatingPoint(object value)
        {
            return ((value is float) || (value is double) || (value is decimal));
        }

        private static decimal GetNumeric(object value)
        {
            if (!IsNumeric(value))
            {
                throw new InvalidOperationException();
            }
            return (decimal)Convert.ChangeType((object)value, TypeCode.Decimal);
        }

        private static long GetInteger(object value)
        {
            if (!IsInteger(value))
            {
                throw new InvalidOperationException();
            }
            return (long)Convert.ChangeType((object)value, TypeCode.Int64);
        }

        private static decimal GetFloatingPoint(object value)
        {
            if (!IsFloatingPoint(value))
            {
                throw new InvalidOperationException();
            }
            return (decimal)Convert.ChangeType((object)value, TypeCode.Decimal);
        }

        #region Convertional operators
        public static implicit operator typedef(T value)
        {
            return new typedef(value);
        }

        public static explicit operator T(typedef value)
        {
            return value.Value;
        }
        #endregion

        #region Unary operators  { +, -, !, ~, ++, --, true, false }
        public static T operator +(typedef operand)
        {
            T temp = operand.m_value;
            if (IsInteger(temp))
            {
                return (T)(object)(+GetInteger(temp));
            }
            else if (IsFloatingPoint(temp))
            {
                return (T)(object)(+GetFloatingPoint(temp));
            }

            return temp;
        }

        public static T operator -(typedef operand)
        {
            T temp = operand.m_value;
            if (IsInteger(temp))
            {
                return (T)(object)(-GetInteger(temp));
            }
            else if (IsFloatingPoint(temp))
            {
                return (T)(object)(-GetFloatingPoint(temp));
            }

            return temp;
        }

        public static T operator !(typedef operand)
        {
            T temp = operand.m_value;
            if (temp is bool)
            {
                return (T)(object)(!((bool)Convert.ChangeType((object)temp, TypeCode.Boolean)));
            }
            else if (IsNumeric(temp))
            {
                decimal val = (decimal)Convert.ChangeType((object)temp, TypeCode.Decimal);
                return (T)(object)((val != 0) ? 0 : val);
            }
            else
            {
                try
                {
                    bool isNull = object.ReferenceEquals(((object)temp), null);
                    return ((isNull) ? ((T)(object)null) : temp);
                }
                catch (Exception)
                {
                    throw new InvalidOperationException();
                }
            }
        }

        public static T operator ~(typedef operand)
        {
            T temp = operand.m_value;
            if (IsInteger(temp))
            {
                return (T)(object)(~GetInteger(temp));
            }

            return temp;
        }

        public static bool operator true(typedef operand)
        {
            T temp = operand.m_value;

            if (temp is bool)
            {
                return (bool)Convert.ChangeType((object)temp, TypeCode.Boolean);
            }
            else if (temp is char)
            {
                return ((char)Convert.ChangeType((object)temp, TypeCode.Char) != '\0');
            }
            else if (temp is string)
            {
                return string.IsNullOrEmpty((string)(object)temp);
            }
            else if (IsInteger(temp))
            {
                return ((long)Convert.ChangeType((object)temp, TypeCode.Int64) > 0);
            }
            else if (IsFloatingPoint(temp))
            {
                return ((decimal)Convert.ChangeType((object)temp, TypeCode.Decimal) > 0);
            }
            else
            {
                try
                {
                    return !object.ReferenceEquals(((object)temp), null);
                }
                catch (Exception)
                {
                    throw new InvalidOperationException();
                }
            }
        }

        public static bool operator false(typedef operand)
        {
            if (operand)
            {
                return false;
            }
            return true;
        }
        #endregion

        #region Binary operators { +, -, *, /, %, &, |, ^, <<, >> }
        public static T operator +(typedef left, typedef right)
        {
            T tempLeft = left.m_value;
            T tempRight = right.m_value;

            if ((tempLeft is string) && (tempRight is string))
            {
                return (T)(object)(((string)(object)tempLeft) + ((string)(object)tempRight));
            }
            else if (IsNumeric(tempLeft) && IsNumeric(tempRight))
            {
                return (T)(object)(GetNumeric((object)tempLeft) + GetNumeric((object)tempRight));
            }

            return tempLeft;
        }

        public static T operator -(typedef left, typedef right)
        {
            T tempLeft = left.m_value;
            T tempRight = right.m_value;

            if (IsNumeric(tempLeft) && IsNumeric(tempRight))
            {
                return (T)(object)(GetNumeric((object)tempLeft) - GetNumeric((object)tempRight));
            }

            return tempLeft;
        }

        public static T operator *(typedef left, typedef right)
        {
            T tempLeft = left.m_value;
            T tempRight = right.m_value;

            if (IsNumeric(tempLeft) && IsNumeric(tempRight))
            {
                return (T)(object)(GetNumeric((object)tempLeft) * GetNumeric((object)tempRight));
            }

            return tempLeft;
        }

        public static T operator /(typedef left, typedef right)
        {
            T tempLeft = left.m_value;
            T tempRight = right.m_value;

            if (IsNumeric(tempLeft) && IsNumeric(tempRight))
            {
                return (T)(object)(GetNumeric((object)tempLeft) / GetNumeric((object)tempRight));
            }

            return tempLeft;
        }

        public static T operator %(typedef left, typedef right)
        {
            T tempLeft = left.m_value;
            T tempRight = right.m_value;

            if (IsNumeric(tempLeft) && IsNumeric(tempRight))
            {
                return (T)(object)(GetNumeric((object)tempLeft) % GetNumeric((object)tempRight));
            }

            return tempLeft;
        }

        public static T operator &(typedef left, typedef right)
        {
            T tempLeft = left.m_value;
            T tempRight = right.m_value;

            if (IsInteger(tempLeft) && IsInteger(tempRight))
            {
                return (T)(object)(GetInteger((object)tempLeft) & GetInteger((object)tempRight));
            }

            return tempLeft;
        }

        public static T operator |(typedef left, typedef right)
        {
            T tempLeft = left.m_value;
            T tempRight = right.m_value;

            if (IsInteger(tempLeft) && IsInteger(tempRight))
            {
                return (T)(object)(GetInteger((object)tempLeft) | GetInteger((object)tempRight));
            }

            return tempLeft;
        }

        public static T operator ^(typedef left, typedef right)
        {
            T tempLeft = left.m_value;
            T tempRight = right.m_value;

            if (IsInteger(tempLeft) && IsInteger(tempRight))
            {
                return (T)(object)(GetInteger((object)tempLeft) ^ GetInteger((object)tempRight));
            }

            return tempLeft;
        }

        public static T operator <<(typedef left, int right)
        {
            T tempLeft = left.m_value;

            if (IsInteger(tempLeft))
            {
                return (T)(object)(GetInteger((object)tempLeft) << right);
            }

            return tempLeft;
        }

        public static T operator >>(typedef left, int right)
        {
            T tempLeft = left.m_value;

            if (IsInteger(tempLeft))
            {
                return (T)(object)(GetInteger((object)tempLeft) >> right);
            }

            return tempLeft;
        }
        #endregion

        #region Comparison operators { ==, !=, <, >, <=, >= }
        public static bool operator ==(typedef left, typedef right)
        {
            //return value1.Value == value2.Value;
            return left.Equals(right);
        }

        public static bool operator !=(typedef left, typedef right)
        {
            //return left.Value != value2.Value;
            return !(left.Equals(right));
        }

        public static bool operator <(typedef left, typedef right)
        {
            T tempLeft = left.m_value;
            T tempRight = right.m_value;

            if (IsNumeric(tempLeft) && IsNumeric(tempRight))
            {
                return (GetNumeric((object)tempLeft) < GetNumeric((object)tempRight));
            }

            return false;
        }

        public static bool operator >(typedef left, typedef right)
        {
            T tempLeft = left.m_value;
            T tempRight = right.m_value;

            if (IsNumeric(tempLeft) && IsNumeric(tempRight))
            {
                return (GetNumeric((object)tempLeft) > GetNumeric((object)tempRight));
            }

            return false;
        }

        public static bool operator <=(typedef left, typedef right)
        {
            T tempLeft = left.m_value;
            T tempRight = right.m_value;

            if (IsNumeric(tempLeft) && IsNumeric(tempRight))
            {
                return (GetNumeric((object)tempLeft) <= GetNumeric((object)tempRight));
            }

            return false;
        }

        public static bool operator >=(typedef left, typedef right)
        {
            T tempLeft = left.m_value;
            T tempRight = right.m_value;

            if (IsNumeric(tempLeft) && IsNumeric(tempRight))
            {
                return (GetNumeric((object)tempLeft) >= GetNumeric((object)tempRight));
            }

            return false;
        }
        #endregion

        #endregion

        #region Properties
        public virtual T Value
        {
            get { return this.m_value; }
            set { this.m_value = value; }
        }
        #endregion
    }
    #endregion





and usage ...





and usage ...

//typedef unsigned char       VTBYTE;
    public class VTBYTE : typedef<byte>
    {
        #region Constructor
        public VTBYTE()
            : base()
        { }

        public VTBYTE(byte value)
            : base(value)
        { }
        #endregion
    }


c# doesn’t support typedef.
c# doesn't support typedef.


这篇关于C#中的typedef的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆