如何在C#类,可以转换为日期时间? [英] How to make class in C#, that can be cast to DateTime?

查看:206
本文介绍了如何在C#类,可以转换为日期时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能让类,可以转换为日期时间。但我需要投我的课,当它包装。例如:

 目标日期1 =新MyDateTime(); 
日期时间日期2 =(DateTime的)DATE1;



我直接需要这个工作的例子。



我知道如何做到这一点,但没有我的包装方法会奏效。我不知道有没有办法做到这一点。



请,帮助。



PS。我需要投直接反对的DateTime。因此,MyDateTime必须之前填充。明确的效果很好,但它并没有帮助,如果你已经打包的对象。而且它有使用普通铸造像

 (DATETIME)(对象)MyDateTime 


解决方案

您看来什么是后继承,能够存储派生类的实例

 流S =新的FileStream():基本类型,像这样的一个变量



事实上,它是一个的FileStream 下引擎盖也不会丢失,只是因为你是指向它与护目镜上。



日期时间结构结构不支持继承 - 所以这是可能的。



另一种明确 定义的转换关键字(语法上的寻找的喜欢的演员)。这可以让你的等级和的DateTime 之间至少有转乘更多的糖分。



http://msdn.microsoft.com/en-us/library/xhbhezf4(ⅴ= vs.71 )的.aspx



这可能看起来像:

 类MyDateTime 
{
私人的DateTime _inner;

公共静态明确经营者的DateTime(MyDateTime MDT)
{
返回mdt._inner;
}
}

您可以做同样的与对方关键字:

 公共静态隐运营商的DateTime(MyDateTime MDT)
{
返回mdt._inner;
}

这则让你做铸造含蓄:

 日期时间日期=新MyDateTime(); 



另一种方法是包装的DateTime 与自己的适配器类,内部使用的DateTime ,然后从该类继承创建 MyDateTime 。 ,那么代替你的代码库使用的DateTime ,你使用这个适配器类。



我见过类似的东西与 SmartDateTime 样式类,其中的DateTime 有一个更好的了解空的,如果它被设置。


How can I make class, that can be cast to DateTime. But I need to cast my class, when it packed. For example:

object date1 = new MyDateTime();
DateTime date2 = (DateTime)date1;

I need directly this working example.

I know how to do it, but my way will work without packing. I'm not sure is there way to do it.

Please, help.

PS. I need cast directly object to DateTime. So, MyDateTime have to be packed before. Explicit works well, but it doesn't help if you have packed object. And it have to cast just using ordinary casting like

(DateTime) (object) MyDateTime

解决方案

What you appear to be after is inheritance, being able to "store" a derived class instance in a variable of the base type like so:

Stream s = new FileStream();

The fact that it is a FileStream under the hood is not lost just because you are pointing to it with the Stream goggles on.

DateTime is a struct, and struct inheritance is not supported - so this is not possible.

An alternative is the explicit keyword for user-defined conversions (syntactically looking like casts). This allows you to at least interchange between your class and DateTime with more sugar.

http://msdn.microsoft.com/en-us/library/xhbhezf4(v=vs.71).aspx

This could look like:

class MyDateTime
{
    private DateTime _inner;

    public static explicit operator DateTime(MyDateTime mdt)
    {
        return mdt._inner;
    }
}

You can do the same with the counterpart implicit keyword:

public static implicit operator DateTime(MyDateTime mdt)
{
    return mdt._inner;
}

That then lets you do the "casting" implicitly:

DateTime date = new MyDateTime();

Another alternative is to wrap DateTime with your own adapter class that internally uses a DateTime and then inherit from this class to create MyDateTime. Then instead of using DateTime in your code base, you use this adapter class.

I've seen similar things with SmartDateTime style classes where the DateTime has a better understanding of nulls and if it was set.

这篇关于如何在C#类,可以转换为日期时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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