惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
The GitHub Blog
The GitHub Blog
F
Fortinet All Blogs
Cloudbric
Cloudbric
P
Palo Alto Networks Blog
T
Threatpost
T
Tor Project blog
T
Tenable Blog
AWS News Blog
AWS News Blog
Project Zero
Project Zero
L
LangChain Blog
Cyberwarzone
Cyberwarzone
Engineering at Meta
Engineering at Meta
雷峰网
雷峰网
C
CERT Recently Published Vulnerability Notes
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Security Latest
Security Latest
云风的 BLOG
云风的 BLOG
I
Intezer
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
A
Arctic Wolf
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
V
Vulnerabilities – Threatpost
C
Cybersecurity and Infrastructure Security Agency CISA
MongoDB | Blog
MongoDB | Blog
aimingoo的专栏
aimingoo的专栏
K
Kaspersky official blog
Jina AI
Jina AI
N
News | PayPal Newsroom
T
The Blog of Author Tim Ferriss
D
DataBreaches.Net
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog
Recorded Future
Recorded Future
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Secure Thoughts
TaoSecurity Blog
TaoSecurity Blog
P
Privacy & Cybersecurity Law Blog
P
Proofpoint News Feed
MyScale Blog
MyScale Blog
IT之家
IT之家
Forbes - Security
Forbes - Security
The Hacker News
The Hacker News
Last Week in AI
Last Week in AI
T
Threat Research - Cisco Blogs
Y
Y Combinator Blog

博客园 - Net205 Blog

我第1个可用的golang小程序 我们怎么做不到呢? Top 7 Coding Standards & Guideline Documents For C#/.NET Developers .Net开发人员必须避免的5个常见的编程错误 You are doing Scrum but the Scrum Master tells the team what to do! Asp.net Mvc中使用HTML 5 data属性 使用Javascript制作一个始终可见的区域 阅读优秀代码是提高开发人员修为的一种捷径[收藏] SQL SERVER – Difference between COUNT(DISTINCT) vs COUNT(ALL) SQL Performance MSSQL删除重复数据 jQuery QUnit 万月薪的英语人是如何练成的!!!讲一口漂亮流利的英语[转] 博文阅读密码验证 - 博客园 博文阅读密码验证 - 博客园 asp.net Interview Questions - Net205 Blog jQuery资源 strip invalid xml characters - Net205 Blog 翻译工具,您选哪个?
使用扩展方法
Net205 Blog · 2011-05-01 · via 博客园 - Net205 Blog

 来自:http://www.dotnetbips.com/articles/dfeb8a51-4b3b-4bc6-aab9-28ac774b1e8c.aspx

比如想使用以下代码显示印度格式的时间(Indian Standard Time:IST):

DateTime dt=DateTime.Now;
DateTime utc
= dt.ToUniversalTime();
DateTime ist
= utc.AddMinutes(330);

使用下面2个扩展方法(注意:要用静态类、静态方法还参数中的this):

namespace Utils
{
public static class DateTimeHelper
{
public static DateTime ToIST(this DateTime dt)
{
DateTime utc
= dt.ToUniversalTime();
return utc.AddMinutes(330);
}
public static bool IsDate(this string s)
{
DateTime dt;
return DateTime.TryParse(s, out dt);
}
}
}

这样引用此命名空间后就可以使用扩展方法了:

注意:

  1. Extension methods can chain themselves. For example you may write something like this: dt.ToIST().ToDDMMYYYY()
    The ToIST() and ToDDMMYYYY() are assumed to be extension methods.
  2. Extension methods can be accessed only when its namespace is in scope
  3. If an extension method has the same signature as any of the instance method of the class it extends then the instance methods take precedence
  4. If two extension methods of a type have same signature them they must be called like normal static methods. For example, if you define two extension methods with same name and signature in two classes say DateTimeHelper1 and dateTimeHelper2, then they can be called  like this - DateTimeHelper1.ToIST(dt)

更多,更全的扩展方法文章:
http://www.cnblogs.com/ldp615/archive/2009/08/07/1541404.html