00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 using System;
00013 using System.Reflection;
00014
00015 namespace Mcs.Epm.MicrosoftProject.mpFx.Client.Shared
00016 {
00017
00018
00019
00020
00021 class InterfaceInspector : MarshalByRefObject, IInterfaceInspector
00022 {
00023 #region Public Methods
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 public bool IsInterfaceImplemented(string filePath, string interfaceName)
00034 {
00035 Type[] types = GetExportedTypes(filePath);
00036
00037 foreach (Type type in types)
00038 {
00039 Type iAgentInterface = type.GetInterface(interfaceName);
00040
00041 if (iAgentInterface != null)
00042 {
00043 return true;
00044 }
00045 }
00046
00047 return false;
00048 }
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059 public bool TryGetTypeNameByFileAndInterface(string filePath, string interfaceName, out string typeName)
00060 {
00061 typeName = string.Empty;
00062
00063 Type[] types = GetExportedTypes(filePath);
00064
00065 foreach (Type type in types)
00066 {
00067 Type iAgentInterface = type.GetInterface(interfaceName);
00068
00069 if (iAgentInterface != null)
00070 {
00071 typeName = type.FullName;
00072 return true;
00073 }
00074 }
00075
00076 return false;
00077 }
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088 public bool TryGetTypeByFileAndInterface(string filePath, string interfaceName, out Type type)
00089 {
00090 Type[] types = GetExportedTypes(filePath);
00091
00092 foreach (Type thisType in types)
00093 {
00094 Type iAgentInterface = thisType.GetInterface(interfaceName);
00095
00096 if (iAgentInterface != null)
00097 {
00098 type = thisType;
00099 return true;
00100 }
00101 }
00102
00103 type = null;
00104 return false;
00105 }
00106
00107
00108
00109
00110
00111 public static IInterfaceInspector Get(AppDomain domain, string inspectorInterfaceName)
00112 {
00113 if (domain == null)
00114 {
00115 throw new ArgumentNullException("domain");
00116 }
00117
00118 if (string.IsNullOrEmpty(inspectorInterfaceName))
00119 {
00120 throw new ArgumentNullException(inspectorInterfaceName);
00121 }
00122
00123 IInterfaceInspector interfaceInspector = domain.CreateInstanceAndUnwrap(GetExecutingAssemblyFullName(),
00124 inspectorInterfaceName) as IInterfaceInspector;
00125
00126 if (interfaceInspector == null)
00127 {
00128 throw new NullReferenceException("IInterfaceInspector");
00129 }
00130
00131 return interfaceInspector;
00132 }
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144 public bool TryGetTypeFromPathInterfaceAndType(string filePath, string interfaceName, string typeName, out Type agentType)
00145 {
00146 Assembly agentAssembly = Assembly.LoadFile(filePath);
00147
00148 agentType = agentAssembly.GetType(typeName);
00149
00150 return agentType != null && agentType.GetInterface(interfaceName) != null;
00151 }
00152
00153 #endregion
00154
00155 #region Private Methods
00156
00157
00158
00159
00160
00161
00162 private static Type[] GetExportedTypes(string filePath)
00163 {
00164 Assembly agentAssembly = Assembly.LoadFrom(filePath);
00165
00166 return agentAssembly.GetExportedTypes();
00167 }
00168
00169
00170
00171
00172
00173 private static string GetExecutingAssemblyFullName()
00174 {
00175 Assembly executingAssembly = Assembly.GetExecutingAssembly();
00176
00177 if (executingAssembly == null || string.IsNullOrEmpty(executingAssembly.FullName))
00178 {
00179 throw new NullReferenceException("executingAssembly");
00180 }
00181 return executingAssembly.FullName;
00182 }
00183
00184
00185 #endregion
00186 }
00187 }