将新属性添加到字符串类C# [英] Add new property to string class C#

查看:92
本文介绍了将新属性添加到字符串类C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以将新属性作为扩展属性添加到字符串类.我正在寻找的是这样的东西

I am wondering if it is possible to add a new property as an extension property to the string class. What I'm looking for is something like this

string.Empty

我想扩展一下,例如:

string.DisplayNone;

我可以向字符串C#类添加扩展属性吗,可以像在执行string.Empty时那样以类似的方式调用它?

Can I add extension properties to the string C# class that I can call in a similar manner like when I do string.Empty?

推荐答案

您只能为对象建立扩展...

You can only build extensions for objects...

类似的东西:

class Program
{
    static void Main(string[] args)
    {
        string x = "Hello World";
        x.DisplayNow();
    }
}

public static class StringExtension
{
    public static void DisplayNow(this string source)
    {
        Console.WriteLine(source);
    }
}

但我从未见过如何扩展从未初始化的结构或类.

but i've never seen how u can extend a struct or a class which has never been initialized.

这篇关于将新属性添加到字符串类C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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