以前VB6里面如果想替换一个字符语句是这样写的:
dim vbvb as string
vbvb="88998989"
Replace(vbvb,"9","8")这样就可以把vbvb里面的9全换成8现在C#是这样的:string cscs;
cscs = "88998989";
cscs.Replace("9","8");
我想实现C#里面像Replace这样的方法用在我写的类上面,直接输入CIS.c_Service.FilterStr("9");就可以把9过滤掉。
CIS是一个实例化的类,c_Service是它的属性怎么才能让c_Service属性具有FilterStr这个方法
以及FilterStr怎么才能像C#的Replace这样,不用再输入要被替换的字符就可以替换并返回字符串?
dim vbvb as string
vbvb="88998989"
Replace(vbvb,"9","8")这样就可以把vbvb里面的9全换成8现在C#是这样的:string cscs;
cscs = "88998989";
cscs.Replace("9","8");
我想实现C#里面像Replace这样的方法用在我写的类上面,直接输入CIS.c_Service.FilterStr("9");就可以把9过滤掉。
CIS是一个实例化的类,c_Service是它的属性怎么才能让c_Service属性具有FilterStr这个方法
以及FilterStr怎么才能像C#的Replace这样,不用再输入要被替换的字符就可以替换并返回字符串?
然后在FilterStr方法里面直接 懒得写了像下面这样吧
class MyService
{
public FilterStr(string str)
{
return str.Replace("9", "新字符串");
}
}
然后在CIS这个类的中
声明MyService c_Service = new MyService ();这样一个变量
就能实现了
但是CIS.c_Service.FilterStr("9");中的c_Service是一个string类型的属性并且有很多这样的属性,我想让每个属性在后面按.都可以出现FilterStr这个方法来进行过滤有办法实现吗,我想微软在做.net FX时不会不让我们这样做吧
我想是不是需要用某种方法加上继承来实现?
C# 3.0新特性之扩展方法
http://msdn.microsoft.com/zh-cn/library/bb383977.aspx
public class CISClass
{
public CISClass(string Value)
{
this.c_Service.nowString=Value
}
private c_Service _c_Service=null
public c_Service c_Service
{
get{ if(_c_Service==null){ _c_Service=new c_Service()} return _c_Service; }
}
}public class c_Service
{
private string _nowString
public string nowString{ get{ return _nowString; } set{ _nowString=value } }
public void FilterStr(string replaceStr)
{
this.nowString.replace(replaceStr,"");
}
}使用:
(new CISClass("88998989")).c_Service.FilterStr("9");
代码是随手写的,可能有错但大至意思如此。
using System.Collections.Generic;
using System.Linq;
using System.Web;/// <summary>
/// Summary description for StringExtend
/// </summary>
public class StringExtension
{
private string _content = string.Empty; public string Content
{
get { return _content; }
set { _content = value; }
} public StringExtension()
{
} public StringExtension(string content)
{
_content = content;
} public string FilterStr(string param)
{
if (string.IsNullOrEmpty(param))
return string.Empty; _content = _content.Replace(param, ""); return _content;
}
}然后用这个类定义你需要的那个属性
不错我要的就是这个,请问在2.0下怎么实现,msdn上面说要用继承其实也不一定要把string类型的再扩展一个方法,我只要我所写的类里面的属性用我自己的方法处理就可以了
继承string
例如: Stringyourown : string
再在里面定义自己的方法filter
下次就用Stringyourown text1=new Stringyourown();
text1.filter()就是了啊
CIS是一个实例化的类,c_Service是它的属性 怎么才能让c_Service属性具有FilterStr这个方法
以及FilterStr怎么才能像C#的Replace这样,不用再输入要被替换的字符就可以替换并返回字符串?
class CIS
{
public CIS()
{}
private _needreplacestring = "";
public string NeedReplaceString
{
get
{
return _needreplacestring ;
}
set
{
_needreplacestring =value;
_service = new Service(needreplacestring );
}
} private Service _service = new Service(needreplacestring );
public Service c_Service
{
get
{
if (_service = null ) _service = new Service(needreplacestring );
return _service;
}
set{_service =value;}
}
}class Service
{
public Service(string needreplacestring)
{
NeedReplaceString = needreplacestring;
}
Public string NeedReplaceString="";
public static string FilterStr(string FilterStr)
{
if (FilterStr == string.empty)
return NeedReplaceString;
else
return NeedReplaceString.Replace(FilterStr,""); }
}