我如何获得或设置自定义类? [英] How would I get or set a custom class?

查看:48
本文介绍了我如何获得或设置自定义类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Codeproject,



对于我的一个C#朋友,我必须创建一个无限整数(Class,DLL)。



然而,有一件事我从来不知道C#。

假设我有一个如下所示的类:



  public 类CustomString 
{

}







我会用这个类创建一个对象:



 CustomString cString =  new  CustomString()

;



然后我会像这样设置课程:



 cString =   Hello; 





我怎样才能使用该类来检索它已经设置的值,然后相应地运行一个void来处理信息,这又是如何得到的?

解决方案

不确定这是否是您要找的。



隐式转换参考 http://msdn.microsoft.com/en-us/library/z5z9kes2.aspx [<一个href =http://msdn.microsoft.com/en-us/library/z5z9kes2.aspxtarget =_ blanktitle =新窗口> ^ ]



  struct  CustomString 
{

/// < 摘要 >
/// 防止< 的默认实例>参见 cref =正在创建CustomString/ > 结构。
/// < / summary >
/// < param < span class =code-summarycomment> name =value > 值。< / param >
private CustomString( string value
{
_value = value ;
}


/// < 摘要 >
< span class =code-summarycomment> /// 内部值
/// < / summary >
private string _value = string .Empty;

/// < 摘要 >
/// 字符串指定的c。
/// < / summary >
/// < param < span class =code-summarycomment> name =c > c 。< / param >
/// < 返回 > < /返回 >
public static 隐式 operator string (CustomString e)
{
return e._value;
}

/// < 摘要 >
/// 对字符串进行清理。
/// < / summary >
/// < param < span class =code-summarycomment> name =e > e 。< / param >
/// < 返回 > < /返回 >
public static implicit operator CustomString(字符串 e)
{
返回 new CustomString(e) ;
}

}





  class  UseCustomString 
{
CustomString e = Some Text ;

void 处理()
{
如果 (e.Equals( Some Text))
{
OtherProcess( E);
}
}

void OtherProcess(CustomString s)
{
// 做点什么
}

}


Hello Codeproject,

For one of my C# friends I had to create an infinite integer(Class, DLL).

However, there is one thing I've never known about C#.
Assuming that I have a class that looks like the following:

public class CustomString
{

}




I would then use this class to create an object out of it:

CustomString cString = new CustomString()

;

And then I would set the class like so:

cString = "Hello";



How could I use the class to retreive the value that it has been set to and then run a void accordingly to handle the information, and how is that with getting too?

解决方案

Not sure if this is what you are looking for or not.

Reference for implicit conversion http://msdn.microsoft.com/en-us/library/z5z9kes2.aspx[^]

struct CustomString
{

    /// <summary>
    /// Prevents a default instance of the <see cref="CustomString"/> struct from being created.
    /// </summary>
    /// <param name="value">The value.</param>
    private CustomString(string value)
    {
        _value = value;
    }


    /// <summary>
    /// The internal value
    /// </summary>
    private string _value = string.Empty;

    /// <summary>
    /// Strings the specified c.
    /// </summary>
    /// <param name="c">The c.</param>
    /// <returns></returns>
    public static implicit operator string(CustomString e)
    {
        return e._value;
    }

    /// <summary>
    /// Customs the string.
    /// </summary>
    /// <param name="e">The e.</param>
    /// <returns></returns>
    public static implicit operator CustomString(string e)
    {
        return new CustomString(e);
    }

}



class UseCustomString
{
    CustomString e = "Some Text";

    void Process()
    {
        if (e.Equals("Some Text"))
        {
            OtherProcess(e);
        }
    }

    void OtherProcess(CustomString s)
    {
        //do something
    }

}


这篇关于我如何获得或设置自定义类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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