/// <summary> /// Creates a COM object and returns the transparent proxy /// which intercepts all calls to the object /// </summary> /// <param name="type">Interface which defines the method and properties to intercept</param> /// <returns>Transparent proxy to the real proxy for the object</returns> /// <remarks> /// The <paramref name="type"/> must be an interface decorated with /// the <see cref="ComProgIdAttribute"/> attribute. /// </remarks> public static object CreateInstance(Type type) { if (type == null) { throw new ArgumentNullException("type"); } if (!type.IsInterface) { throw new ArgumentException("The specified type must be an interface.", "type"); } ComProgIdAttribute progID = ComProgIdAttribute.GetAttribute(type); if (progID == null || String.IsNullOrEmpty(progID.Value)) { throw new ArgumentException("The specified type must define a ComProgId attribute.", "type"); } Type comType = Type.GetTypeFromProgID(progID.Value, true); object comObject = Activator.CreateInstance(comType); if (comObject == null) { string err = string.Format("Unable to create an instance of the specified COM server \"{0}\".", progID.Value); throw new TypeLoadException(err); } COMWrapper wrapper = new COMWrapper(comObject, type); return(wrapper.GetTransparentProxy()); }
/// <summary> /// Wrap an object and return the transparent proxy which intercepts all calls /// to the object /// </summary> /// <param name="comObject">An object to intercept</param> /// <param name="type">Interface which defines the method and properties to intercept</param> /// <returns>Transparent proxy to the real proxy for the object</returns> public static object Wrap(object comObject, Type type) { if (comObject == null) { throw new ArgumentNullException("comObject"); } if (type == null) { throw new ArgumentNullException("type"); } COMWrapper wrapper = new COMWrapper(comObject, type); return(wrapper.GetTransparentProxy()); }