求逻辑组合方法????

yun166 2010-10-18 09:44:55

public class test
{
public void start()
{
Console.WriteLine("开始");
}
public void stop()
{
Console.WriteLine("结束");
}

public void show1()
{
start();

//其他实现

stop();
}
public void show2(string str)
{
start();

//其他实现

stop();
}
public void show3(int num,string str)
{
start();

//其他实现

stop();
}
//…
}


上面的代码,无论调用show1、show2、show3…shown,都要调用start和stop函数,
有没有办法,使得这种重复啰嗦的写法简单化,也就是每次调用show1…shown都会调用先start和后stop
谢谢!
...全文
168 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
deyygywxf 2010-10-19
  • 打赏
  • 举报
回复
void Test(object state)
{
Console.WriteLine(state.ToString());
}
void StartThreads()
{
System.Threading.ThreadPool.QueueUserWorkItem(Test, 123);
new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(Test)).Start("123");
}
或委托
bloodish 2010-10-19
  • 打赏
  • 举报
回复
佩服gomoku
每次看你的回复都获益良多.
gomoku 2010-10-19
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 problc 的回复:]
思想上比较像AOP。
仅仅单个问题来说用AOP可能有点杀鸡用牛刀。
如果想了解一下AOP,可以搜"C# AOP realproxy"
[/Quote]


public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
My my = MyLogWrapper.Wrap(new My()); //<----
MessageBox.Show(my.Test1() + " " + my.Test2 + " " + my.Test3());
}
}

class My : Component
{
public string Test1() { return "hello"; }
public string Test2 { get { return "world"; } }

[MyLogWrapper.SuppressContextLogAttribute] //<----
public string Test3() { return "what?"; }
}

public class MyLogWrapper
{
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property )]
public class SuppressContextLogAttribute : Attribute
{
}

public static T Wrap<T>(T target) where T : MarshalByRefObject
{
return new MyProxy(typeof(T), target).GetTransparentProxy() as T;
}

private class MyProxy : RealProxy
{
public MyProxy(Type t, MarshalByRefObject target) : base(t)
{
this.target = target;
}

public override IMessage Invoke(IMessage msg)
{
MethodBase method = (msg as IMethodMessage).MethodBase;
bool logging = method.GetCustomAttributes(typeof(SuppressContextLogAttribute), false).Length == 0;
if (logging) System.Diagnostics.Trace.WriteLine("Start " + method.Name);
IMessage result = RemotingServices.ExecuteMessage(target, msg as IMethodCallMessage);
if (logging) System.Diagnostics.Trace.WriteLine("Stop " + method.Name);
return result;
}
private MarshalByRefObject target;
}
}
bloodish 2010-10-19
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 duanzhi1984 的回复:]
用委托的话,因三个SHOW方法的类参数数量与类型不同。那么在作委托时有问题吗?
[/Quote]

泛型委托+参数封装,参看7#
duanzhi1984 2010-10-19
  • 打赏
  • 举报
回复
用委托的话,因三个SHOW方法的类参数数量与类型不同。那么在作委托时有问题吗?

请教
bloodish 2010-10-19
  • 打赏
  • 举报
回复
换个思路考虑这个问题,Start,Stop可以提取到Show方法外面做,而把Show的操作作为一个委托传入

将你要传的参数定义到一个Struct或Class里,然后作为泛型T showParam传入


private void ShowEx<T>(Action<T> showAction, T showParam)
{
Start();
if (showAction != null)
{
showAction(showParam);
}
Stop();
}
zenjj 2010-10-19
  • 打赏
  • 举报
回复
帮你顶~~~~~~~~~~~~~~~
@井九 2010-10-18
  • 打赏
  • 举报
回复
思想上比较像AOP。
仅仅单个问题来说用AOP可能有点杀鸡用牛刀。
如果想了解一下AOP,可以搜"C# AOP realproxy"
yun166 2010-10-18
  • 打赏
  • 举报
回复
委托的办法我想过,就是每次要转换类型,还有别的吗?
有些函数上面有个[],这种是不是添加属性,可以实现这个功能吗?
int64 2010-10-18
  • 打赏
  • 举报
回复
学习了,还没用过委托
wuyq11 2010-10-18
  • 打赏
  • 举报
回复
void Test(object state)
{
Console.WriteLine(state.ToString());
}
void StartThreads()
{
System.Threading.ThreadPool.QueueUserWorkItem(Test, 123);
new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(Test)).Start("123");
}
或委托
threenewbee 2010-10-18
  • 打赏
  • 举报
回复
可以。使用委托:

public delegate void ToDoMethod();

public void Show(ToDoMethod t, object[] Param)
{
start();
if (t != null) t(Param);
stop();
}

调用:
Show(delegate() { MessageBox("todo"); }, "1", "2");

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧