组件描述
该组件为Windows Phone 7 本身C#框架扩展了一系列方法,可以使你在编写代码的时候减少重复复制,并且增加了许多通用功能,使你的编写代码的时候可以更加流畅和得以应手。
扩展类别
该组件是将我们日常常用到的数据类型或者集合等操作再一次封装成易于使用的静态方法,分类为如下几大类:
- String 字符串扩展
- DateTime 日期扩展
- Guid 全局唯一标识符扩展
- IEnumerable 集合扩展
- Object 对象扩展
- Stream 流扩展
- Uri 统一资源标识符扩展
- Bool 真假“是否”扩展
- Int 整型扩展
扩展方法体
以下为每个静态类的扩展方法列表
StringExtensions
静态方法成员截图:

Format 代码:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
public
static
string
Format(
this
string
self,
params
object
[]args)
{
if
(self
==
null
)
{
throw
new
ArgumentNullException(
"
format
"
);
}
return
string
.Format(self,args);
}
HasValue 代码:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
public
static
bool
HasValue(
this
string
self)
{
return
!
string
.IsNullOrEmpty(self);
}
IsNullOrEmpty代码:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
public
static
bool
IsNullOrEmpty(
this
string
self)
{
return
string
.IsNullOrEmpty(self);
}
IsValidEmailAddress代码:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
public
static
bool
IsValidEmailAddress(
this
string
self)
{
Regexregex
=
new
Regex(
@"
^[/w-/.]+@([/w-]+/.)+[/w-]{2,4}$
"
);
return
regex.IsMatch(self);
}
Split 代码:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
public
static
IEnumerable
<
string
>
Split(
this
string
self,
char
separator)
{
return
self.Split(
new
char
[]{separator});
}
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
public
static
IEnumerable
<
string
>
Split(
this
string
self,
string
separator)
{
return
self.Split(
new
string
[]{separator},StringSplitOptions.None);
}
ToInt 代码:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
public
static
int
ToInt(
this
string
self)
{
int
num;
if
(
!
int
.TryParse(self,
out
num))
{
throw
new
InvalidOperationException(
"
Valueisnotvalid.
"
);
}
return
num;
}
Trim 代码:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
public
static
string
Trim(
this
string
self,
char
character)
{
return
self.Trim(
new
char
[]{character});
}
DateTimeExtensions
静态方法成员截图:

AddWeek 代码:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
public
static
DateTimeAddWeek(
this
DateTimedateTime)
{
return
dateTime.AddDays(
7.0
);
}
ToUnixTimestamp代码:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
public
static
long
ToUnixTimestamp(
this
DateTimedate)
{
DateTimetime
=
new
DateTime(
0x7b2
,
1
,
1
,
0
,
0
,
0
);
TimeSpanspan
=
(TimeSpan)(date
-
time);
return
(
long
)span.TotalSeconds;
}
Tip:上面的time 是1/1/1970 12:00:00 AM
GuidExtensions
静态方法成员截图:

IsGuidEmpty 代码 :
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
public
static
bool
IsGuidEmpty(
this
Guidself)
{
return
(self
==
Guid.Empty);
}
RemoveHyphen 代码:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
public
static
string
RemoveHyphen(
this
Guidself)
{
return
self.ToString().Replace(
"
-
"
,
""
);
}
IEnumerableExtensions
静态方法成员截图:

ExistsIn<T> 代码:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
public
static
bool
ExistsIn
<
T
>
(
this
Tobj,IEnumerable
<
T
>
collection)
{
return
Enumerable.Contains
<
T
>
(collection,obj);
}
ForEach<T> 代码:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
public
static
void
ForEach
<
T
>
(
this
IEnumerable
<
T
>
sequence,Action
<
T
>
action)
{
if
(sequence
==
null
)
{
throw
new
ArgumentNullException(
"
Thesecuenceisnull!
"
);
}
if
(action
==
null
)
{
throw
new
ArgumentNullException(
"
Theactionisnull!
"
);
}
foreach
(Tlocal
in
sequence)
{
action(local);
}
}
IsNullOrEmpty 代码:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
public
static
bool
IsNullOrEmpty
<
T
>
(
this
IEnumerable
<
T
>
obj)
{
if
(
!
obj.IsNull())
{
return
(Enumerable.Count
<
T
>
(obj)
==
0
);
}
return
true
;
}
ToObservableCollection<T> 代码:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
public
static
ObservableCollection
<
T
>
ToObservableCollection
<
T
>
(
this
IEnumerable
<
T
>
source)
{
ObservableCollection
<
T
>
observables
=
new
ObservableCollection
<
T
>
();
source.ForEach
<
T
>
(
new
Action
<
T
>
(observables.Add));
return
observables;
}
ObjectExtensions
静态方法成员截图:

In 代码:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
public
static
bool
In(
this
object
self,IEnumerableenumerable)
{
return
(enumerable.IsNotNull()
&&
Enumerable.Contains
<
object
>
(Enumerable.Cast
<
object
>
(enumerable),self));
}
IsNotNull 代码:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
public
static
bool
IsNotNull(
this
object
self)
{
return
(self
!=
null
);
}
IsNull 代码:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
public
static
bool
IsNull(
this
object
self)
{
return
(self
==
null
);
}
NullTolerantEquals 代码:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
public
static
bool
NullTolerantEquals(
this
object
self,
object
obj)
{
if
(self.IsNull()
&&
obj.IsNotNull())
{
return
false
;
}
if
(self.IsNotNull()
&&
obj.IsNull())
{
return
false
;
}
return
((self.IsNull()
&&
obj.IsNull())
||
self.Equals(obj));
}
StreamExtensions
静态方法成员列表截图:

EqualsStream 代码:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
public
static
bool
EqualsStream(
this
StreamoriginalStream,StreamstreamToCompareWith)
{
return
originalStream.EqualsStream(streamToCompareWith,Math.Max(originalStream.Length,streamToCompareWith.Length));
}
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
public
static
bool
EqualsStream(
this
StreamoriginalStream,StreamstreamToCompareWith,
long
readLength)
{
originalStream.Position
=
0L
;
streamToCompareWith.Position
=
0L
;
for
(
int
i
=
0
;i
<
readLength;i
++
)
{
if
(originalStream.ReadByte()
!=
streamToCompareWith.ReadByte())
{
return
false
;
}
}
return
true
;
}
ReadAllText 代码:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
public
static
string
ReadAllText(
this
Streamstream)
{
using
(StreamReaderreader
=
new
StreamReader(stream))
{
return
reader.ReadToEnd();
}
}
ToByteArray 代码:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
public
static
byte
[]ToByteArray(
this
Streamstream)
{
MemoryStreamwriteStream
=
new
MemoryStream();
StreamHelper.CopyStream(stream,writeStream,
true
);
return
writeStream.ToArray();
}
UriExtensions
静态方法成员列表截图:

Parameters 代码:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
public
static
Dictionary
<
string
,
string
>
Parameters(
this
Uriself)
{
if
(self.IsNull())
{
throw
new
ArgumentException(
"
Urican'tbenull.
"
);
}
if
(
string
.IsNullOrEmpty(self.Query))
{
return
new
Dictionary
<
string
,
string
>
();
}
if
(CS$
<>
9__CachedAnonymousMethodDelegate2
==
null
)
{
CS$
<>
9__CachedAnonymousMethodDelegate2
=
new
Func
<
string
,
string
>
(
null
,(IntPtr)
<
Parameters
>
b__0);
}
if
(CS$
<>
9__CachedAnonymousMethodDelegate3
==
null
)
{
CS$
<>
9__CachedAnonymousMethodDelegate3
=
new
Func
<
string
,
string
>
(
null
,(IntPtr)
<
Parameters
>
b__1);
}
return
Enumerable.ToDictionary
<
string
,
string
,
string
>
(self.Query.Substring(
1
).Split(
new
char
[]{
'
&
'
}),CS$
<>
9__CachedAnonymousMethodDelegate2,CS$
<>
9__CachedAnonymousMethodDelegate3);
}
BoolExtensions
静态方法成员列表截图:

IsFalse 代码:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
public
static
bool
IsFalse(
this
bool
self)
{
return
!
self;
}
IsTrue 代码:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
public
static
bool
IsTrue(
this
bool
self)
{
return
self;
}
IntExtensions
静态方法成员列表截图:

IsWithin 代码:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
public
static
bool
IsWithin(
this
int
self,
int
minimum,
int
maximum)
{
if
(minimum
>
maximum)
{
throw
new
ArgumentException(
"
minimummustbeoflessvaluethanmaximum.
"
);
}
return
((self
>=
minimum)
&&
(self
<=
maximum));
}
组件下载:Extension