Posts
[C#]RaiseEvent Extension Method (二)
public static void RaiseEvent(this object obj, EventHandler handler, Func<EventArgs> func) { if (handler == null) return; handler(obj, func()); } public static void RaiseEvent<TEventArgs>(this object obj, EventHandler<TEventArgs> handler, TEventArgs e) where TEventArgs : EventArgs { RaiseEvent(obj, handler, () => e); } public static void RaiseEvent<TEventArgs>(this object obj, EventHandler<TEventArgs> handler,Func<TEventArgs> func) where TEventArgs : EventArgs { if (handler == null) return; handler(obj, func()); } }</pre></div> namespace ConsoleApplication14 { class Program { static void Main(string[] args) { Person Larry = new Person(); Larry.
read morePosts
[C#][Linq]Linq to Wikipedia
private IQueryable<WikipediaKeywordSearchResult>KeyWordSearch(string keyWord) { return from item in (new WikipediaContext()).KeywordSearch where item.Keyword == keyWord select item; }</pre></div>
read morePosts
[C#]取得網卡的IPV6位置
namespace ConsoleApplication13 { class Program { static void Main(string[] args) { foreach (var ip in GetLocalIPV6IP()) { Console.WriteLine(ip); } }
private static IEnumerable<String> GetLocalIPV6IP() { return (from adapter in NetworkInterface.GetAllNetworkInterfaces() where adapter .NetworkInterfaceType == NetworkInterfaceType.Ethernet from AddressInfo in adapter.GetIPProperties().UnicastAddresses.OfType<UnicastIPAddressInformation>() where AddressInfo.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6 let ipAddress = AddressInfo.Address.ToString() select ipAddress); } } }
read morePosts
[.Net Concept]理解事件的宣告方式與用法
#region Event event NameChangingEventHandler NameChanging; event NameChangedEventHandler NameChanged; #endregion</pre> #region Event event EventHandler<FriendEventArgs> FriendAdded; #endregion
#region Protected Method protected void OnFriendAdded(FriendEventArgs e) { if (FriendAdded == null) return; FriendAdded(this, e); } #endregion
#region Public Method public void AddFriend(Person friend) { Friends.Add(friend); OnFriendAdded(new FriendEventArgs(friend.Name)); } #endregion
…
class FriendEventArgs : EventArgs { #region Var private String _name; #endregion
#region Property public String Name { get { if (_name == null) return String.
read morePosts
[C#]DictService
namespace DictServceDemo { public partial class Form1 : Form {
#region Var private DictService _dictService; #endregion #region Private Property public DictService m_DictService { get { if (_dictService == null) _dictService = new DictService(); return _dictService; } } #endregion #region Constructor public Form1() { InitializeComponent(); } #endregion #region Event Process private void Form1_Load(object sender, EventArgs e) { cbxDict.DisplayMember = "Name"; cbxDict.DataSource = m_DictService.DictionaryList(); cbxStrategy.DisplayMember = "Id"; cbxStrategy.DataSource = m_DictService.StrategyList(); } private void lbxResult_SelectedIndexChanged(object sender, EventArgs e) { if (cbxDict.
read morePosts
[C#]Stream.Write Extension Method
public static class StreamExtension { public static void Write(this Stream targetStream, byte[] buffer) { if (!targetStream.CanWrite) throw new ArgumentException(“targetStream”, “Unwritable stream”);
targetStream.Write(buffer, 0, buffer.Length); } public static void Write(this Stream targetStream, Stream sourceStream) { if (!targetStream.CanWrite) throw new ArgumentException("targetStream", "Unwritable stream"); if (sourceStream == null) throw new ArgumentNullException("sourceStream"); if (!sourceStream.CanRead) throw new ArgumentException("sourceStream", "Unreadable stream"); targetStream.Write(sourceStream, 1024, null); } public static void Write(this Stream targetStream, Stream sourceStream, int bufferSize, Action<object, System.
read morePosts
[C#][Linq]LINQ To WMI
foreach (Win32_UserAccount account in query) { Console.WriteLine(account.Name); } } }
read morePosts
[C#][Linq]BLinq - Linq To Bing
private IEnumerable<ImageSearchResult> SearchImages(string keyWord) { return from item in m_Bing.Images where item.Query == keyWord select item; }</pre>
read more