代码如下:
namespace x{
public class A{
public string sayHello(){
/*
当classs B的实例调用class A的这个方法时
我如何在此处获取classs B的实例的句柄并
能classs B的实例的方法Add()进行调用
*/
return "hello evey one";
}
}
}
namespace y{
using x;
public class B{
int _n;
public int n{
get{return _n;}
set{_n=value;}
}
public int Add(){
return ++n;
}
static void Main(){
A a=new A();
string xx=a.sayHello();
}
}
}
哪位兄弟知道,请指点一二
用事件实现如下
1
using System;
2
using System.Drawing;
3
using System.Collections;
4
using System.ComponentModel;
5
using System.Windows.Forms;
6
using System.Data;
7
using log4net;
8
using System.Reflection;
9
10
namespace WY.RisingSun.WMS.UI
11
{
12
public enum UIStyle{
13
UIdefault,
14
UIoffice2003,
15
UIwinXP,
16
UIvs2005
17
}
18
19
public class TabedFrmEventArgs : System.EventArgs{
20
string[] _fireFrmName;
21
bool _isGetFocus=false;
22
private TabedFrmEventArgs(){}
23
24
/// <summary>
25
///
26
/// </summary>
27
/// <param name="isGetFocus">which tabed form will Activte</param>
28
/// <param name="frmName">fireed from names</param>
29
public TabedFrmEventArgs(bool isGetFocus,params string[] frmName){
30
_isGetFocus=isGetFocus;
31
_fireFrmName=frmName;
32
}
33
34
public string[] FireEventFromName{
35
get{
36
return _fireFrmName;
37
}
38
}
39
40
public bool IsGetFocus{
41
get{
42
return _isGetFocus;
43
}
44
}
45
46
}
47
48
/// <summary>
49
/// form base
50
/// </summary>
51
public class frmBase : System.Windows.Forms.Form
52
{
53
private System.ComponentModel.Container components = null;
54
55
private UIStyle _style=UIStyle.UIdefault;
56
57
public frmBase(){
58
this.OtherTabedFrmChanged+=new ChangedFrmEventHandler(UpdateTabedFrm);
59
}
60
61
62
/// <summary>
63
/// 清理所有正在使用的资源。
64
/// </summary>
65
protected override void Dispose( bool disposing )
66
{
67
if( disposing )
68
{
69
if (components != null)
70
{
71
components.Dispose();
72
}
73
}
74
base.Dispose( disposing );
75
}
76
77
private void InitializeComponent() {
78
//
79
// frmBase
80
//
81
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
82
this.ClientSize = new System.Drawing.Size(292, 273);
83
this.Name = "frmBase";
84
this.Text = "change this text as you will";
85
86
}
87
88
89
#region Mointor Height or Width
90
public static int MonitorHeight{
91
get{
92
return System.Windows.Forms.SystemInformation.PrimaryMonitorSize.Height;
93
}
94
}
95
96
public static int MonitorWidth{
97
get{
98
return System.Windows.Forms.SystemInformation.PrimaryMonitorSize.Width;
99
}
100
}
101
102
#endregion
103
104
#region UI Style
105
/// <summary>
106
/// get or set style on contrls of Infragistics
107
/// </summary>
108
protected virtual UIStyle ContrlSyle{
109
get{return _style;}
110
// set{_style=value;}
111
}
112
113
/// <summary>
114
/// change Infragistics contrls style
115
/// </summary>
116
/// <param name="contrlStyle"></param>
117
protected virtual void ChageContrlsStyle(UIStyle contrlStyle){
118
switch(contrlStyle) {
119
case UIStyle.UIoffice2003:
120
121
break;
122
case UIStyle.UIvs2005:
123
124
break;
125
case UIStyle.UIwinXP:
126
127
break;
128
default:
129
break;
130
131
}
132
}
133
134
#endregion
135
136
#region a wrapper for log4net
137
138
private static ILog _log;
139
140
/// <summary>
141
/// how to use: set current type handle first then invoke ILog instance method
142
/// e.g.
143
/// LOG=LogManager.GetLogger(CurrentType);
144
/// LOG.Info(Msg);
145
/// LOG.Error(Msg);
146
/// LOG.Debug(Msg);
147
/// etc
148
/// </summary>
149
public static ILog LOG{
150
get{
151
return _log;
152
}
153
set{
154
_log=value;
155
}
156
157
}
158
159
#endregion
160
161
#region provide a way will be used to communion with different driven classes
162
163
protected delegate void ChangedFrmEventHandler(object sender, TabedFrmEventArgs evt);
164
165
protected event ChangedFrmEventHandler OtherTabedFrmChanged;
166
167
internal virtual void OnOtherTabedFrmChanged(TabedFrmEventArgs evt) {
168
if (OtherTabedFrmChanged != null)
169
OtherTabedFrmChanged(this, evt);
170
}
171
protected virtual void UpdateTabedFrm(object sender, TabedFrmEventArgs evt){
172
try{
173
string[] frmName=evt.FireEventFromName;
174
if (frmName==null||frmName.Length==0) return;
175
//do upload frm info
176
frmBase frm=sender as frmBase;
177
frmMain mdiContainer=(frmMain)frm.MdiParent;
178
for (int i=0;i<frmName.Length;i++) {
179
foreach (frmBase fb in mdiContainer.MdiChildren) {
180
if (frmName[i]==fb.Name) {
181
fb.updateFrmData(sender,evt);
182
if(evt.IsGetFocus)fb.Activate();
183
break;
184
}
185
}
186
}
187
//Console.WriteLine(sender.GetType());
188
}catch(Exception exc){
189
//do log it
190
191
}
192
193
}
194
195
/// <summary>
196
/// this method must be override by devin class when fire this method
197
/// </summary>
198
/// <param name="sender">pass event sender to other tabed form</param>
199
/// <param name="evt"></param>
200
internal virtual void updateFrmData(object sender,TabedFrmEventArgs evt){
201
202
}
203
204
#endregion
205
206
}
207
}
208
using System;2
using System.Drawing;3
using System.Collections;4
using System.ComponentModel;5
using System.Windows.Forms;6
using System.Data;7
using log4net;8
using System.Reflection;9

10
namespace WY.RisingSun.WMS.UI11
{12
public enum UIStyle{13
UIdefault,14
UIoffice2003,15
UIwinXP,16
UIvs200517
}18

19
public class TabedFrmEventArgs : System.EventArgs{20
string[] _fireFrmName;21
bool _isGetFocus=false;22
private TabedFrmEventArgs(){}23
24
/// <summary>25
/// 26
/// </summary>27
/// <param name="isGetFocus">which tabed form will Activte</param>28
/// <param name="frmName">fireed from names</param>29
public TabedFrmEventArgs(bool isGetFocus,params string[] frmName){30
_isGetFocus=isGetFocus;31
_fireFrmName=frmName;32
}33

34
public string[] FireEventFromName{35
get{36
return _fireFrmName;37
}38
}39
40
public bool IsGetFocus{41
get{42
return _isGetFocus;43
}44
}45

46
}47

48
/// <summary>49
/// form base50
/// </summary>51
public class frmBase : System.Windows.Forms.Form52
{53
private System.ComponentModel.Container components = null;54
55
private UIStyle _style=UIStyle.UIdefault;56

57
public frmBase(){58
this.OtherTabedFrmChanged+=new ChangedFrmEventHandler(UpdateTabedFrm);59
}60
61

62
/// <summary>63
/// 清理所有正在使用的资源。64
/// </summary>65
protected override void Dispose( bool disposing )66
{67
if( disposing )68
{69
if (components != null) 70
{71
components.Dispose();72
}73
}74
base.Dispose( disposing );75
}76

77
private void InitializeComponent() {78
// 79
// frmBase80
// 81
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);82
this.ClientSize = new System.Drawing.Size(292, 273);83
this.Name = "frmBase";84
this.Text = "change this text as you will";85

86
}87

88

89
#region Mointor Height or Width90
public static int MonitorHeight{91
get{92
return System.Windows.Forms.SystemInformation.PrimaryMonitorSize.Height;93
}94
}95

96
public static int MonitorWidth{97
get{98
return System.Windows.Forms.SystemInformation.PrimaryMonitorSize.Width;99
}100
}101

102
#endregion103

104
#region UI Style105
/// <summary>106
/// get or set style on contrls of Infragistics 107
/// </summary>108
protected virtual UIStyle ContrlSyle{109
get{return _style;}110
// set{_style=value;}111
}112
113
/// <summary>114
/// change Infragistics contrls style115
/// </summary>116
/// <param name="contrlStyle"></param>117
protected virtual void ChageContrlsStyle(UIStyle contrlStyle){118
switch(contrlStyle) {119
case UIStyle.UIoffice2003:120

121
break;122
case UIStyle.UIvs2005:123

124
break;125
case UIStyle.UIwinXP:126

127
break;128
default:129
break;130
131
}132
}133

134
#endregion135

136
#region a wrapper for log4net137

138
private static ILog _log;139
140
/// <summary>141
/// how to use: set current type handle first then invoke ILog instance method142
/// e.g.143
/// LOG=LogManager.GetLogger(CurrentType);144
/// LOG.Info(Msg);145
/// LOG.Error(Msg);146
/// LOG.Debug(Msg);147
/// etc
148
/// </summary>149
public static ILog LOG{150
get{151
return _log;152
}153
set{154
_log=value;155
}156

157
}158

159
#endregion160
161
#region provide a way will be used to communion with different driven classes162
163
protected delegate void ChangedFrmEventHandler(object sender, TabedFrmEventArgs evt);164
165
protected event ChangedFrmEventHandler OtherTabedFrmChanged;166
167
internal virtual void OnOtherTabedFrmChanged(TabedFrmEventArgs evt) {168
if (OtherTabedFrmChanged != null)169
OtherTabedFrmChanged(this, evt);170
}171
protected virtual void UpdateTabedFrm(object sender, TabedFrmEventArgs evt){172
try{173
string[] frmName=evt.FireEventFromName;174
if (frmName==null||frmName.Length==0) return;175
//do upload frm info176
frmBase frm=sender as frmBase;177
frmMain mdiContainer=(frmMain)frm.MdiParent;178
for (int i=0;i<frmName.Length;i++) {179
foreach (frmBase fb in mdiContainer.MdiChildren) {180
if (frmName[i]==fb.Name) {181
fb.updateFrmData(sender,evt);182
if(evt.IsGetFocus)fb.Activate();183
break;184
}185
}186
}187
//Console.WriteLine(sender.GetType());188
}catch(Exception exc){189
//do log it190

191
}192
193
}194
195
/// <summary>196
/// this method must be override by devin class when fire this method197
/// </summary>198
/// <param name="sender">pass event sender to other tabed form</param>199
/// <param name="evt"></param>200
internal virtual void updateFrmData(object sender,TabedFrmEventArgs evt){201
202
}203

204
#endregion205

206
}207
}208

1
using System;
2
using System.Collections;
3
using System.ComponentModel;
4
using System.Drawing;
5
using System.Windows.Forms;
6
7
namespace WY.RisingSun.WMS.UI
8
{
9
public class tt1 : WY.RisingSun.WMS.UI.frmBase
10
{
11
private System.Windows.Forms.TextBox textBox1;
12
private System.Windows.Forms.Button button1;
13
private System.ComponentModel.IContainer components = null;
14
15
public tt1()
16
{
17
InitializeComponent();
18
}
19
20
/// <summary>
21
/// 清理所有正在使用的资源。
22
/// </summary>
23
protected override void Dispose( bool disposing )
24
{
25
if( disposing )
26
{
27
if (components != null)
28
{
29
components.Dispose();
30
}
31
}
32
base.Dispose( disposing );
33
}
34
35
#region 设计器生成的代码
36
/// <summary>
37
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
38
/// 此方法的内容。
39
/// </summary>
40
private void InitializeComponent()
41
{
42
this.textBox1 = new System.Windows.Forms.TextBox();
43
this.button1 = new System.Windows.Forms.Button();
44
this.SuspendLayout();
45
//
46
// textBox1
47
//
48
this.textBox1.Location = new System.Drawing.Point(0, 8);
49
this.textBox1.Name = "textBox1";
50
this.textBox1.Size = new System.Drawing.Size(184, 21);
51
this.textBox1.TabIndex = 0;
52
this.textBox1.Text = "textBox1";
53
//
54
// button1
55
//
56
this.button1.Location = new System.Drawing.Point(80, 112);
57
this.button1.Name = "button1";
58
this.button1.TabIndex = 1;
59
this.button1.Text = "引发事件";
60
this.button1.Click += new System.EventHandler(this.button1_Click);
61
//
62
// tt1
63
//
64
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
65
this.ClientSize = new System.Drawing.Size(292, 273);
66
this.Controls.Add(this.button1);
67
this.Controls.Add(this.textBox1);
68
this.Name = "tt1";
69
this.Text = "driven class2";
70
this.ResumeLayout(false);
71
72
}
73
#endregion
74
75
private void button1_Click(object sender, System.EventArgs e) {
76
//base.OnOtherTabedFrmChanged(new TabedFrmEventArgs());
77
//this.OnOtherTabedFrmChanged(new WY.RisingSun.WMS.UI.TabedFrmEventArgs());
78
Form1 frm1=new Form1();
79
base.OnOtherTabedFrmChanged(new TabedFrmEventArgs(true,"tt2"));
80
}
81
}
82
}
83
84
using System;2
using System.Collections;3
using System.ComponentModel;4
using System.Drawing;5
using System.Windows.Forms;6

7
namespace WY.RisingSun.WMS.UI8
{9
public class tt1 : WY.RisingSun.WMS.UI.frmBase10
{11
private System.Windows.Forms.TextBox textBox1;12
private System.Windows.Forms.Button button1;13
private System.ComponentModel.IContainer components = null;14

15
public tt1()16
{17
InitializeComponent();18
}19

20
/// <summary>21
/// 清理所有正在使用的资源。22
/// </summary>23
protected override void Dispose( bool disposing )24
{25
if( disposing )26
{27
if (components != null) 28
{29
components.Dispose();30
}31
}32
base.Dispose( disposing );33
}34

35
#region 设计器生成的代码36
/// <summary>37
/// 设计器支持所需的方法 - 不要使用代码编辑器修改38
/// 此方法的内容。39
/// </summary>40
private void InitializeComponent()41
{42
this.textBox1 = new System.Windows.Forms.TextBox();43
this.button1 = new System.Windows.Forms.Button();44
this.SuspendLayout();45
// 46
// textBox147
// 48
this.textBox1.Location = new System.Drawing.Point(0, 8);49
this.textBox1.Name = "textBox1";50
this.textBox1.Size = new System.Drawing.Size(184, 21);51
this.textBox1.TabIndex = 0;52
this.textBox1.Text = "textBox1";53
// 54
// button155
// 56
this.button1.Location = new System.Drawing.Point(80, 112);57
this.button1.Name = "button1";58
this.button1.TabIndex = 1;59
this.button1.Text = "引发事件";60
this.button1.Click += new System.EventHandler(this.button1_Click);61
// 62
// tt163
// 64
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);65
this.ClientSize = new System.Drawing.Size(292, 273);66
this.Controls.Add(this.button1);67
this.Controls.Add(this.textBox1);68
this.Name = "tt1";69
this.Text = "driven class2";70
this.ResumeLayout(false);71

72
}73
#endregion74

75
private void button1_Click(object sender, System.EventArgs e) {76
//base.OnOtherTabedFrmChanged(new TabedFrmEventArgs());77
//this.OnOtherTabedFrmChanged(new WY.RisingSun.WMS.UI.TabedFrmEventArgs());78
Form1 frm1=new Form1();79
base.OnOtherTabedFrmChanged(new TabedFrmEventArgs(true,"tt2"));80
}81
}82
}83

84

1
using System;
2
using System.Collections;
3
using System.ComponentModel;
4
using System.Drawing;
5
using System.Windows.Forms;
6
7
namespace WY.RisingSun.WMS.UI
8
{
9
public class tt2 : WY.RisingSun.WMS.UI.frmBase
10
{
11
private System.Windows.Forms.Label label1;
12
private System.ComponentModel.IContainer components = null;
13
14
public tt2()
15
{
16
// 该调用是 Windows 窗体设计器所必需的。
17
InitializeComponent();
18
19
// TODO: 在 InitializeComponent 调用后添加任何初始化
20
}
21
22
/// <summary>
23
/// 清理所有正在使用的资源。
24
/// </summary>
25
protected override void Dispose( bool disposing )
26
{
27
if( disposing )
28
{
29
if (components != null)
30
{
31
components.Dispose();
32
}
33
}
34
base.Dispose( disposing );
35
}
36
37
internal override void updateFrmData(object sender, TabedFrmEventArgs evt) {
38
string xx=sender.GetType().Name;
39
this.label1.Text="hello i love you forever!!!! the "+xx+" tabed form fire this method";
40
this.label1.ForeColor=System.Drawing.Color.Red;
41
}
42
43
44
#region 设计器生成的代码
45
/// <summary>
46
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
47
/// 此方法的内容。
48
/// </summary>
49
private void InitializeComponent()
50
{
51
this.label1 = new System.Windows.Forms.Label();
52
this.SuspendLayout();
53
//
54
// label1
55
//
56
this.label1.Location = new System.Drawing.Point(56, 40);
57
this.label1.Name = "label1";
58
this.label1.Size = new System.Drawing.Size(472, 40);
59
this.label1.TabIndex = 0;
60
this.label1.Text = "it will be chanaged ";
61
//
62
// tt2
63
//
64
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
65
this.ClientSize = new System.Drawing.Size(608, 269);
66
this.Controls.Add(this.label1);
67
this.Name = "tt2";
68
this.Text = "tt2";
69
this.ResumeLayout(false);
70
71
}
72
#endregion
73
}
74
}
75
76
using System;2
using System.Collections;3
using System.ComponentModel;4
using System.Drawing;5
using System.Windows.Forms;6

7
namespace WY.RisingSun.WMS.UI8
{9
public class tt2 : WY.RisingSun.WMS.UI.frmBase10
{11
private System.Windows.Forms.Label label1;12
private System.ComponentModel.IContainer components = null;13

14
public tt2()15
{16
// 该调用是 Windows 窗体设计器所必需的。17
InitializeComponent();18

19
// TODO: 在 InitializeComponent 调用后添加任何初始化20
}21

22
/// <summary>23
/// 清理所有正在使用的资源。24
/// </summary>25
protected override void Dispose( bool disposing )26
{27
if( disposing )28
{29
if (components != null) 30
{31
components.Dispose();32
}33
}34
base.Dispose( disposing );35
}36
37
internal override void updateFrmData(object sender, TabedFrmEventArgs evt) {38
string xx=sender.GetType().Name;39
this.label1.Text="hello i love you forever!!!! the "+xx+" tabed form fire this method";40
this.label1.ForeColor=System.Drawing.Color.Red;41
}42

43

44
#region 设计器生成的代码45
/// <summary>46
/// 设计器支持所需的方法 - 不要使用代码编辑器修改47
/// 此方法的内容。48
/// </summary>49
private void InitializeComponent()50
{51
this.label1 = new System.Windows.Forms.Label();52
this.SuspendLayout();53
// 54
// label155
// 56
this.label1.Location = new System.Drawing.Point(56, 40);57
this.label1.Name = "label1";58
this.label1.Size = new System.Drawing.Size(472, 40);59
this.label1.TabIndex = 0;60
this.label1.Text = "it will be chanaged ";61
// 62
// tt263
// 64
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);65
this.ClientSize = new System.Drawing.Size(608, 269);66
this.Controls.Add(this.label1);67
this.Name = "tt2";68
this.Text = "tt2";69
this.ResumeLayout(false);70

71
}72
#endregion73
}74
}75

76

