- 积分
- 24557
- 明经币
- 个
- 注册时间
- 2004-3-17
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
楼主 |
发表于 2015-4-17 23:08:17
|
显示全部楼层
三、反射类AcObject继承于DynamicObject,以便在.Net4.0中使用动态特性
- using System;
- using System.Collections.Generic;
- using System.Reflection;
- using System.Runtime.InteropServices;
- using System.Text.RegularExpressions;
- using System.Dynamic;
- namespace TlsCad.Common.Runtime
- {
- public class AcObject : DynamicObject
- {
- public static dynamic Application
- { private set; get; }
- public static dynamic Preferences
- {
- get { return Application.Preferences; }
- }
- public static AcVersion Version
- { private set; get; }
- public Type Type
- { private set; get; }
- public object Value
- { private set; get; }
- #region Wrapper
- protected AcObject() { }
- public static dynamic Wrapper(dynamic obj, Type type)
- {
- if (obj != null && Marshal.IsComObject(obj))
- {
- var wrapper =
- new AcObject
- {
- Value = obj,
- Type =
- Regex.IsMatch(type.Name, "^Acad.*?$") ?
- type.GetInterfaces()[0] :
- Version.GetType(obj)
- };
- if (wrapper.Type.GetMethod("Item") != null)
- return wrapper.AsItems();
- return wrapper;
- }
- else
- {
- return obj;
- }
- }
- public static dynamic Wrapper(object obj)
- {
- return Wrapper(obj, Version.GetType(obj));
- }
- #endregion
- #region Invoke
- private dynamic Invoke(MethodInfo mi, object[] args)
- {
- object obj = mi.Invoke(Value, args);
- return Wrapper(obj, mi.ReturnType);
- }
- public dynamic InvokeMember(string name, params object[] args)
- {
- try
- {
- //获取接口函数
- MethodInfo mi = Type.GetMethod(name);
- //配置参数
- var pars = mi.GetParameters();
- object[] realargs = new object[pars.Length];
- Array.Copy(args, realargs, args.Length);
- for (int i = args.Length; i < realargs.Length; i++)
- realargs = pars.IsOptional ? Type.Missing : null;
- List<dynamic> lst = new List<dynamic> { Invoke(mi, realargs) };
- for (int i = args.Length; i < realargs.Length; i++)
- {
- if (pars.IsOut)
- lst.Add(Wrapper(realargs, pars.ParameterType));
- }
- if (lst.Count == 1)
- return lst[0];
- return lst;
- }
- catch
- {
- return null;
- }
- }
- #endregion
- #region Property
- public AcItems AsItems()
- {
- return
- new AcItems
- {
- Value = Value,
- Type = Type
- };
- }
- public dynamic GetMember(string name)
- {
- try
- {
- var mi = Type.GetMember("get_" + name)[0] as MethodInfo;
- return Invoke(mi, new object[0]);
- }
- catch
- {
- return null;
- }
- }
- public void SetMember(string name, object value)
- {
- try
- {
- var mi = Type.GetMember("set_" + name)[0] as MethodInfo;
- mi.Invoke(Value, new object[] { value });
- }
- catch
- { }
- }
- #endregion
- #region App
- public static bool GetApp()
- {
- object app;
- try
- {
- app = Marshal.GetActiveObject("AutoCad.Application");
- return GetApp(app);
- }
- catch
- {
- return false;
- }
- }
- public static bool GetApp(object app)
- {
- try
- {
- Version = AcVersion.FromApp(app);
- Version.LoadAssembly();
- Application = (AcObject)Wrapper(app);
- return true;
- }
- catch
- {
- return false;
- }
- }
- public static bool OpenApp(AcVersion ver)
- {
- try
- {
- var app =
- Activator.CreateInstance(Type.GetTypeFromProgID("AutoCad.Application." + ver.ProgId));
- return GetApp(app);
- }
- catch
- {
- return false;
- }
- }
- #endregion
- #region DynamicObject
- public override bool TryGetMember(GetMemberBinder binder, out object result)
- {
- result = GetMember(binder.Name);
- return true;
- }
- public override bool TrySetMember(SetMemberBinder binder, object value)
- {
- SetMember(binder.Name, value);
- return true;
- }
- public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
- {
- result = InvokeMember(binder.Name, args);
- return true;
- }
- #endregion
- }
- }
|
|