Use Lambda expressions, extension methods, and generics to add methods for objects

Source: Internet
Author: User

Question 1:

    public void AsycStartDispatcher(Dispatcher dispatcher)
{
var thread = new Thread(
(input) =>
{
var dispatch = input as Dispatcher;
if (dispatch != null) dispatch.Start();
});
thread.Start(dispatcher);
}

Consider the above Code. We can see that we have written so many lines of code. In fact, it is just a simple task to complete, that is, to execute dispatch in a separate thread. the Start () method; is there a better way? Can I use only one row as below to complete this function?

    public void AsycStartDispatcher(Dispatcher dispatcher)
{
dispatcher.AsycExec(obj => obj.Start());
}

Problem 1 solution:

If we use Lambda expressions, extension methods, and generics to add a method to an object, we can solve this problem. The specific code is as follows:

    public static class ObjectExtensions
{
public static void AsycExec<T>(this T obj, Action<T> action)
{
action.BeginInvoke(obj, null, null);
}
}

However, we have not considered thread Exception Handling here.

Question 2:

    public void StartAllDispatcher(IEnumerable<Dispatcher> dispatchers)
{
foreach (var dispatcher in dispatchers)
{
dispatcher.Start();
}
}

public void AsycStartAllDispatcher(IEnumerable<Dispatcher> dispatchers)
{
foreach (var dispatcher in dispatchers)
{
var thread = new Thread(
(input)
=>
{
var dispatch = input as Dispatcher;
if (dispatch != null) dispatch.Start();
});
thread.Start(dispatcher);
}
}

Can we directly execute a method for the entire Enumerable object without using Foreach for the above Code? The following is the example:

    public void TestForeach(IEnumerable<Dispatcher> dispatchers)
{
dispatchers.ExecEach(obj => obj.Start());

dispatchers.AsycExecEach(obj => obj.Start());
}

Problem 2 solution:

    public static class ObjectExtensions
{
public static void ExecEach<T>(this IEnumerable<T> objs, Action<T> action)
{
foreach (var obj in objs)
{
action(obj);
}
}

public static void AsycExecEach<T>(this IEnumerable<T> objs, Action<T> action)
{
foreach (var obj in objs)
{
action.BeginInvoke(obj, null, null);
}
}
}

Question 3:

    lock (s_ObjectCachePools)
{
if (!s_ObjectCachePools.ContainsKey(poolName))
{
s_ObjectCachePools.Add(poolName, new List<Object>());
}

s_ObjectCachePools[poolName].Add(result);
}

For the s_ObjectCachePools object above, the lock (s_ObjectCachePools) statement is used for each operation. Can we write another statement? As shown below:

    s_ObjectCachePools.LockExec(objs =>
{
if (!objs.ContainsKey(poolName))
{
objs.Add(poolName, new List<Object>());
}

objs[poolName].Add(result);
});

Problem 3 solution:

    public static class ObjectExtensions
{
public static void LockExec<T>(this T obj, Action<T> action) where T : class
{
lock (obj)
{
action(obj);
}
}
}

Total Solution

    public static class ObjectExtensions
{
public static void Exec<T>(this T obj, Action<T> action)
{
action(obj);
}

public static void AsycExec<T>(this T obj, Action<T> action)
{
action.BeginInvoke(obj,null,null);
}

public static void ExecEach<T>(this IEnumerable<T> objs, Action<T> action)
{
foreach (var obj in objs)
{
action(obj);
}
}

public static void AsycExecEach<T>(this IEnumerable<T> objs, Action<T> action)
{
foreach (var obj in objs)
{
action.BeginInvoke(obj,null,null);
}
}

public static void LockExec<T>(this T obj, Action<T> action) where T:class
{
lock (obj)
{
action(obj);
}
}
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.