在Web应用程序中实现IConvertible接口 [英] Implementing IConvertible interface in Web Application

查看:70
本文介绍了在Web应用程序中实现IConvertible接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实现IConvertible接口,需要将我的对象[decimal type]转换为int数据类型:

public TypeCode GetTypeCode()

{



//抛出新的NotImplementedException();

}



请帮我调用GetTypeCode() ,通过启用我的存储十进制类型值的对象为整数类型值。

Implemented IConvertible interface, need to convert my object[decimal type] into int datatype :
public TypeCode GetTypeCode()
{

//throw new NotImplementedException();
}

Please help me in calling the GetTypeCode() ,by enabling my object which stores decimal type values into integer type value.

推荐答案

正如您所提到的,您实现了IConvertible,这意味着您当前的类型是一个保存您的小数的类类型。

如果上述假设为真,那么你只需要这样做:



As you mentioned you implemented IConvertible which means your current type is some class that holds your decimal type.
If the above assumption is true then you just need to do this:

class DecimalType : IConvertible
    {
        private decimal decimalVar;
        public DecimalType(decimal d)
        {
            this.decimalVar = d;
        }

        public TypeCode GetTypeCode()
        {
            return TypeCode.Object;
        }

        public short ToInt16(IFormatProvider provider) //Or implement other functions that is necessary
        {
            return Convert.ToInt16(this.decimalVar);
        }
        //leave the other IConvertible methods as default
}

class ProgramConvert
    {
        static void Main(string[] args)
        {
            var decimalType = new DecimalType(20);
            Console.WriteLine(Convert.ToInt16(decimalType).GetType());
            Console.ReadLine();
        }

    }


这篇关于在Web应用程序中实现IConvertible接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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