为MMC编写服务器控制管理单元。插件方式 (MMC3.0 ,反射, installutil ,c#,KB907265,Enterprise Library)
经常性做服务端开发的,如果在服务器端 有一个很好用户管理界面,那将是非常完美的事情,服务端的管理界面,不求华丽,但求实用,而MMC控制管理台将是很专业的选择。
SMCCAddIn 工程 是一个主工程,是MMC的插件,也是AssemblyAddIn 的容器。在bin/debug 下有个AddIns 目录,是存放插件dll的。 AssemblyAddIn 是一个插件实例工程。
本源代码 在我去掉一些敏感词语前是编译通过的(编译我也试过了,可以通过),可以运行(修改后没有运行)。 MMC 是windows系统带的管理单元控制台,比如我们常用的IIS6.0的管理界面就是基于MMC的一个管理单元。这个控制台在3.0 之后的版本支持用.net代码写的插件,以前只能用c++写。在win2003R2之后的版本已经是 MMC3.0 了,在这之前的版本需要打补丁KB907265(比如WindowsXP-KB907265-x86-CHS-mmc3.0),有关MMC的知识请google一下吧,把自己开发好的dll注册到MMC里面,要运行: installutil mydll.dll,卸载用: installutil /u mydll.dll。
本源代码原来是用在一个项目中,这些源代码 还是在摸索阶段写的。所以有些粗糙。学习了Enterprise Library 的一些设计思想,但又没有完全用好。作为插件方式的那个主体工程还有很多不够完美的地方。请大家见谅,现在天气热了,人懒了,不想再去改进了,就这样发上来共享一下吧。
主要的还是贴源代码,这个符合大家的心意。
代码会陆续贴出来:
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using SMCC.Attributes;
5
using SMCC;
6
using Microsoft.ManagementConsole;
7
using System.Windows.Forms;
8
9
namespace AssemblyAddIn
10
{
11
[ActionDefinition(typeof(MyNode))]
12
public class MyActions :AbstractAction
13
{
14
[ActionMethod("Show Refesh", "Show Refesh desp", -1, "aab", false)]
15
public void ShowRefesh(ActionBase action, Status status)
16
{
17
Helper.MessageBox("hello,Fire Action On ShowRefesh", "Hello", MessageBoxButtons.OK);
18
}
19
20
[ActionMethod("Show Common Diag", "Show Diag desp", -1, "daaab", false)]
21
public void ShowMyDiag(ActionBase action, Status status)
22
{
23
MyModeDiag f = new MyModeDiag();
24
25
f.ShowDialog();
26
}
27
28
[ActionMethod("Show Helper Diag", "Show Diag desp", -1, "daaab", false)]
29
public void ShowHelperModeDiag(ActionBase action, Status status)
30
{
31
MyModeDiag f = new MyModeDiag();
32
33
Helper.ShowDialog(f);
34
}
35
36
//
37
[ActionMethod("My属性", "My属性描述", -1, "sdfee", false)]
38
public override void ShowPropertySheet(ActionBase action, Status status)
39
{
40
//必须调用基类的方法。
41
base.ShowPropertySheet(action, status);
42
}
43
44
[ActionMethod("Delete", "Delete desp", -1, "addee56", false)]
45
public void DeleteNode(ActionBase action, Status status)
46
{
47
if (DialogResult.OK == Helper.MessageBox("你确定要删除该节点?", "再次确认", MessageBoxButtons.OKCancel))
48
{
49
50
51
}
52
}
53
54
55
56
}
57
}
58
using System;2
using System.Collections.Generic;3
using System.Text;4
using SMCC.Attributes;5
using SMCC;6
using Microsoft.ManagementConsole;7
using System.Windows.Forms;8

9
namespace AssemblyAddIn10
{11
[ActionDefinition(typeof(MyNode))]12
public class MyActions :AbstractAction13
{14
[ActionMethod("Show Refesh", "Show Refesh desp", -1, "aab", false)]15
public void ShowRefesh(ActionBase action, Status status)16
{17
Helper.MessageBox("hello,Fire Action On ShowRefesh", "Hello", MessageBoxButtons.OK);18
}19

20
[ActionMethod("Show Common Diag", "Show Diag desp", -1, "daaab", false)]21
public void ShowMyDiag(ActionBase action, Status status)22
{23
MyModeDiag f = new MyModeDiag();24
25
f.ShowDialog();26
}27

28
[ActionMethod("Show Helper Diag", "Show Diag desp", -1, "daaab", false)]29
public void ShowHelperModeDiag(ActionBase action, Status status)30
{31
MyModeDiag f = new MyModeDiag();32

33
Helper.ShowDialog(f);34
}35

36
//37
[ActionMethod("My属性", "My属性描述", -1, "sdfee", false)]38
public override void ShowPropertySheet(ActionBase action, Status status)39
{40
//必须调用基类的方法。41
base.ShowPropertySheet(action, status);42
}43

44
[ActionMethod("Delete", "Delete desp", -1, "addee56", false)]45
public void DeleteNode(ActionBase action, Status status)46
{47
if (DialogResult.OK == Helper.MessageBox("你确定要删除该节点?", "再次确认", MessageBoxButtons.OKCancel))48
{49
50
51
}52
}53

54
55

56
}57
}58



using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using SMCC.Attributes;

namespace SMCC
{
/// <summary>
/// Binds together source events and listener handler methods through reflection.
/// </summary>
public class EventMethodBinder
{
/// <summary>
/// Binds together source events and listener handler methods through reflection.
/// </summary>
/// <param name="eventSource">Object containing events attributed with <see cref="InstrumentationProviderAttribute"></see>.</param>
/// <param name="eventListener">Object containing handler methods attribute with <see cref="InstrumentationConsumerAttribute"></see>.</param>
public void Bind(object eventSource, object eventListener)
{
EventBinder binder = new EventBinder(eventSource, eventListener);
Dictionary<string, List<EventInfo>> eventMap = GetAddInEvents(eventSource);
Dictionary<string, List<MethodInfo>> listenerMap = GetAddInListeners(eventListener);
foreach (string topic in eventMap.Keys)
{
if (listenerMap.ContainsKey(topic) == false) continue;
List<EventInfo> sourceEvents = eventMap[topic];
List<MethodInfo> targetMethods = listenerMap[topic];
foreach (EventInfo sourceEvent in sourceEvents)
{
foreach (MethodInfo targetMethod in targetMethods)
{
binder.Bind(sourceEvent, targetMethod);
}
}
}
}
delegate T[] MemberFinder<T>(Type t);
EventInfo[] GetEventInfo(Type t) { return t.GetEvents(); }
Dictionary<string, List<EventInfo>> GetAddInEvents(object eventSource)
{
return GetAttributedMembers<EventInfo,
EventProviderAttribute>(eventSource, delegate(Type t) { return GetEventInfo(t); });
}
MethodInfo[] GetMethodInfo(Type t) { return t.GetMethods(); }
Dictionary<string, List<MethodInfo>> GetAddInListeners(object eventListener)
{
return GetAttributedMembers<MethodInfo,
EventConsumerAttribute>(eventListener, delegate(Type t) { return GetMethodInfo(t); });
}
Dictionary<string, List<TMemberInfoType>>
GetAttributedMembers<TMemberInfoType, TAttributeType>(object target, MemberFinder<TMemberInfoType> memberFinder)
where TMemberInfoType : MemberInfo
where TAttributeType : BaseEventAttribute
{
Dictionary<string, List<TMemberInfoType>> memberInfoList =
new Dictionary<string, List<TMemberInfoType>>();
Type type = target.GetType();
MemberInfo[] memberInfos = memberFinder(type);
foreach (MemberInfo memberInfo in memberInfos)
{
object[] attributes = memberInfo.GetCustomAttributes(typeof(TAttributeType), false);
foreach (BaseEventAttribute attribute in attributes)
{
if (memberInfoList.ContainsKey(attribute.SubjectName) == false)
{
memberInfoList.Add(attribute.SubjectName, new List<TMemberInfoType>());
}
List<TMemberInfoType> list = memberInfoList[attribute.SubjectName];
list.Add((TMemberInfoType)memberInfo);
}
}
return memberInfoList;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace SMCC.Attributes
{
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
public class ActionDefinitionAttribute : Attribute
{
protected Type nodeType;

public Type NodeType { get { return nodeType; } set { nodeType = value; } }


public ActionDefinitionAttribute( Type nodeType)
{
this.nodeType = nodeType;
}



}
}


using System;
using System.Collections.Generic;
using SMCC.Attributes;
using System.Reflection;
using Microsoft.ManagementConsole;
namespace SMCC
{

public class ActionBuilderStrategy : BuilderStrategy
{

public ActionBuilderStrategy(IBuilderStrategy next) : base(typeof(AbstractAction), next) { }
public override object BuildUp(BuildContext context)
{
Type toBuildtype=context.Type;
Type nodeType;
AbstractAction toBuildEntity;
List<ActionItem> actionItems=new List<ActionItem>();
ScopeNodeEntity entity;
if (baseType.IsAssignableFrom(toBuildtype))
{
object[] attributes = toBuildtype.GetCustomAttributes(typeof(ActionDefinitionAttribute), false);
if (attributes.Length > 0)
{

foreach (Attribute attribute in attributes)
{

//CustomViewControl
toBuildEntity = (AbstractAction)Activator.CreateInstance(toBuildtype);

ActionDefinitionAttribute definitionAttribute = (ActionDefinitionAttribute)attribute;
nodeType = definitionAttribute.NodeType;

//-------------------------------
MethodInfo[] methodInfos = toBuildtype.GetMethods();
foreach (MethodInfo methodInfo in methodInfos)
{
object[] methodAttributes = methodInfo.GetCustomAttributes(typeof(ActionMethodAttribute), false);
if (methodAttributes.Length == 1)
{
ActionMethodAttribute methodAttribute = (ActionMethodAttribute)methodAttributes[0];

actionItems.Add(
new ActionItem(methodAttribute.DisplayName,
methodAttribute.Description,
methodAttribute.ImageIndex,
methodAttribute.Tag,
methodAttribute.IsSync,
methodInfo,
toBuildEntity
)
);
//-----------

}

}
//-------------------------------

//------ICustomInitialization--实现自定义的初始化内容
if (toBuildEntity is ICustomInitialization)
{
(toBuildEntity as ICustomInitialization).CustomInitialize();
}
//---------------
if (actionItems.Count > 0)//if count is less than 0,no need to set the entity.
{
entity = context.GetEntity(nodeType);
//toBuildEntity.Node = entity.Node; //这里要传入node 信息,给 AbstractAction中的node.ShowPropertySheet("属性")使用。
//foreach (ActionItem item in actionItems)
//{
// if (item.IsSync)
// {
// entity.Node.ActionsPaneItems.Add(new SyncAction(item.DisplayName,
// item.Description,
// item.ImageIndex,
// item.Tag));
// }
// else
// {
// entity.Node.ActionsPaneItems.Add(new Action(item.DisplayName,
// item.Description,
// item.ImageIndex,
// item.Tag));
// }
//}
entity.ActionItemList.AddRange(actionItems);
entity.NodeType = nodeType;

//ActionEventProvider actionEventProvider = (ActionEventProvider)entity.Node.GetEventProvider();
//actionEventProvider.Binder(new PairKey<AbstractAction, List<ActionItem>>(toBuildEntity, actionItems));

//SetEntity(ref entitys, entity, nodeType);
//---------------
}

}
}
else
{
Helper.PrintMessage("RootNodeBuilderStrategy can't build without RootNodeDefinitionAttribute");
}


}
return base.BuildUp(context);
}

}
}源代码地址http://download.csdn.net/source/529003



浙公网安备 33010602011771号