当前位置:首 页 > 攻城湿 >Windows Phone > 查看文章

Windows Phone扩展框架组件

Windows Phone 你是第1916个围观者 4条评论 供稿者: 标签:, , ,

组件描述:该组件为Windows Phone 7 本身C#框架扩展了一系列方法,可以使你在编写代码的时候减少重复复制,并且增加了许多通用功能,使你的编写代码的时候可以更加流畅和得以应手。

扩展类别
该组件是将我们日常常用到的数据类型或者集合等操作再一次封装成易于使用的静态方法,分类为如下几大类:
String 字符串扩展
DateTime 日期扩展
Guid 全局唯一标识符扩展
IEnumerable 集合扩展
Object 对象扩展
Stream 流扩展
Uri 统一资源标识符扩展
Bool 真假“是否”扩展
Int 整型扩展
扩展方法体
以下为每个静态类的扩展方法列表

StringExtensions

Format 代码:

public static string Format(this string self, params object[] args)
{
    if (self == null)
    {
        throw new ArgumentNullException("format");
    }
    return string.Format(self, args);
}

HasValue 代码:

public static bool HasValue(this string self)
{
    return !string.IsNullOrEmpty(self);
}

IsNullOrEmpty代码:

public static bool IsNullOrEmpty(this string self)
{
    return string.IsNullOrEmpty(self);
}

IsValidEmailAddress代码:

public static bool IsValidEmailAddress(this string self)
{
    Regex regex = new Regex(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$");
    return regex.IsMatch(self);
}

Split 代码:

public static IEnumerable<string> Split(this string self, char separator)
{
    return self.Split(new char[] { separator });
 
}
</string>
public static IEnumerable<string> Split(this string self, string separator)
{
    return self.Split(new string[] { separator }, StringSplitOptions.None);
}
</string>

ToInt 代码:

public static int ToInt(this string self)
{
    int num;
    if (!int.TryParse(self, out num))
    {
        throw new InvalidOperationException("Value is not valid.");
    }
    return num;
}

Trim 代码:

public static string Trim(this string self, char character)
{
    return self.Trim(new char[] { character });
}

DateTimeExtensions

AddWeek 代码:

public static DateTime AddWeek(this DateTime dateTime)
{
    return dateTime.AddDays(7.0);
}

ToUnixTimestamp代码:

public static long ToUnixTimestamp(this DateTime date)
{
    DateTime time = new DateTime(0x7b2, 1, 1, 0, 0, 0);
    TimeSpan span = (TimeSpan) (date - time);
    return (long) span.TotalSeconds;
}

Tip:上面的time 是1/1/1970 12:00:00 AM

GuidExtensions

IsGuidEmpty 代码 :

public static bool IsGuidEmpty(this Guid self)
{
    return (self == Guid.Empty);
}

RemoveHyphen 代码:

public static string RemoveHyphen(this Guid self)
{
    return self.ToString().Replace("-", "");
}

IEnumerableExtensions

ExistsIn 代码:

public static bool ExistsIn<t>(this T obj, IEnumerable</t><t> collection)
{
    return Enumerable.Contains</t><t>(collection, obj);
}
</t>

ForEach 代码:

public static void ForEach<t>(this IEnumerable</t><t> sequence, Action</t><t> action)
{
    if (sequence == null)
    {
        throw new ArgumentNullException("The secuence is null!");
    }
    if (action == null)
    {
        throw new ArgumentNullException("The action is null!");
    }
    foreach (T local in sequence)
    {
        action(local);
    }
}
</t>

IsNullOrEmpty 代码:

public static bool IsNullOrEmpty<t>(this IEnumerable</t><t> obj)
{
    if (!obj.IsNull())
    {
        return (Enumerable.Count</t><t>(obj) == 0);
    }
    return true;
}
</t>

ToObservableCollection 代码:

public static ObservableCollection<t> ToObservableCollection</t><t>(this IEnumerable</t><t> source)
{
    ObservableCollection</t><t> observables = new ObservableCollection</t><t>();
    source.ForEach</t><t>(new Action</t><t>(observables.Add));
    return observables;
}
</t>

ObjectExtensions

In 代码:

public static bool In(this object self, IEnumerable enumerable)
{
    return (enumerable.IsNotNull() && Enumerable.Contains<object>(Enumerable.Cast<object>(enumerable), self));
}
</object></object>

IsNotNull 代码:

public static bool IsNotNull(this object self)
{
    return (self != null);
}

IsNull 代码:

public static bool IsNull(this object self)
{
    return (self == null);
}

NullTolerantEquals 代码:

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 代码:

public static bool EqualsStream(this Stream originalStream, Stream streamToCompareWith)
{
    return originalStream.EqualsStream(streamToCompareWith, Math.Max(originalStream.Length, streamToCompareWith.Length));
}

 
 
 
public static bool EqualsStream(this Stream originalStream, Stream streamToCompareWith, 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 代码:

public static string ReadAllText(this Stream stream)
{
    using (StreamReader reader = new StreamReader(stream))
    {
        return reader.ReadToEnd();
    }
}

ToByteArray 代码:

public static byte[] ToByteArray(this Stream stream)
{
    MemoryStream writeStream = new MemoryStream();
    StreamHelper.CopyStream(stream, writeStream, true);
    return writeStream.ToArray();
}

UriExtensions

Parameters 代码:

public static Dictionary<string , string> Parameters(this Uri self)
{
    if (self.IsNull())
    {
        throw new ArgumentException("Uri can't be null.");
    }
    if (string.IsNullOrEmpty(self.Query))
    {
        return new Dictionary</string><string , string>();
    }
    if (CS$<>9__CachedAnonymousMethodDelegate2 == null)
    {
        CS$<>9__CachedAnonymousMethodDelegate2 = new Func</string><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);
}
 
</string></parameters></string></parameters></string>

BoolExtensions

IsFalse 代码:

public static bool IsFalse(this bool self)
{
    return !self;
}

IsTrue 代码:

public static bool IsTrue(this bool self)
{
    return self;
}

IntExtensions

IsWithin 代码:

public static bool IsWithin(this int self, int minimum, int maximum)
{
    if (minimum > maximum)
    {
        throw new ArgumentException("minimum must be of less value than maximum.");
    }
    return ((self >= minimum) && (self < = maximum));
}

这家伙很懒,什么都没写!

—— zhaorong

zhaorong
你可能也喜欢Related Posts
众说纷纭Comments
大眼 可爱 大笑 坏笑 害羞 发怒 折磨 快哭了 大哭 白眼 晕 流汗 困 腼腆 惊讶 憨笑 色 得意 骷髅 囧 睡觉 眨眼 亲亲 疑问 闭嘴 难过 淡定 抗议 鄙视 猪头
小提示:直接粘贴图片到输入框试试
努力发送中...
  1. 1 楼 Carte R4

    Great, thanks for sharing this blog article.Really looking forward to read more. Fantastic.

    2013年03月23日 06:07:58 回复 取消回复
  2. 2 楼 r4 3ds

    I think this is a real great article post.Much thanks again. Want more.

    2013年03月24日 09:38:29 回复 取消回复
  3. 3 楼 r4

    This is a really good read for me, Must admit that you are one of the best bloggers I ever saw.Thanks for posting this informative article.

    2013年03月26日 11:23:04 回复 取消回复
  • 评论最多
  • 最新评论
  • 随机文章
footer logo
未经许可请勿自行使用、转载、修改、复制、发行、出售、发表或以其它方式利用本网站之内容
Copyright © zhaorong All Rights Reserved. 滇ICP备15006105号-1