public void DaServerTest() { DaServer _daServer = new DaServer(); Assert.IsNotNull(_daServer); Assert.IsTrue(File.Exists("CommServer_Main.log"), $"Cannot find the file in {Environment.CurrentDirectory}"); Assert.IsFalse(Environment.Is64BitProcess); RegistrationServices _registrationServices = new RegistrationServices(); //GetProgIdForType string _progId = _registrationServices.GetProgIdForType(typeof(DaServer)); Assert.AreEqual <string>("CAS.CommServer.DA.Server.NETServer.DaServer", _progId); //GetRegistrableTypesInAssembly Type[] _typesToRegister = _registrationServices.GetRegistrableTypesInAssembly(typeof(DaServer).Assembly); Assert.IsNotNull(_typesToRegister); Assert.AreEqual <int>(1, _typesToRegister.Length); Assert.AreEqual <string>("CAS.CommServer.DA.Server.NETServer.DaServer", _typesToRegister[0].FullName); //TypeRepresentsComType Assert.IsFalse(_registrationServices.TypeRepresentsComType(typeof(DaServer))); Assert.IsFalse(_registrationServices.TypeRepresentsComType(typeof(IOPCCommon))); Assert.IsFalse(_registrationServices.TypeRepresentsComType(typeof(IOPCCommon))); Assert.IsFalse(_registrationServices.TypeRepresentsComType(typeof(IOPCWrappedServer))); //TypeRequiresRegistration Assert.IsTrue(_registrationServices.TypeRequiresRegistration(typeof(DaServer))); object[] _attributes = typeof(DaServer).GetCustomAttributes(typeof(GuidAttribute), false); Assert.AreEqual <int>(1, _attributes.Length); Assert.AreEqual <string>("BE77A3C7-D2B7-44E7-B943-B978C1C87E5A", ((GuidAttribute)_attributes[0]).Value.ToUpper()); }
public void CheckAssemblySCValidity(Assembly asm) { Type[] types = null; bool flag = true; ArrayList list = null; RegistrationServices services = new RegistrationServices(); try { types = asm.GetTypes(); } catch (ReflectionTypeLoadException exception) { types = exception.Types; } foreach (Type type in types) { if (((null != type) && type.IsClass) && type.IsSubclassOf(typeof(ServicedComponent))) { if (!services.TypeRequiresRegistration(type) && !type.IsAbstract) { flag = false; if (list == null) { list = new ArrayList(); } RegistrationErrorInfo info = new RegistrationErrorInfo(null, null, type.ToString(), -2147467259); list.Add(info); } ClassInterfaceType classInterfaceType = ServicedComponentInfo.GetClassInterfaceType(type); foreach (MethodInfo info2 in type.GetMethods()) { if (ReflectionCache.ConvertToInterfaceMI(info2) == null) { if (ServicedComponentInfo.HasSpecialMethodAttributes(info2)) { this.ReportWarning(Resource.FormatString("Reg_NoClassInterfaceSecure", type.FullName, info2.Name)); } if ((classInterfaceType == ClassInterfaceType.AutoDispatch) && ServicedComponentInfo.IsMethodAutoDone(info2)) { this.ReportWarning(Resource.FormatString("Reg_NoClassInterface", type.FullName, info2.Name)); } } } } } if (!flag) { RegistrationErrorInfo[] errorInfo = (RegistrationErrorInfo[])list.ToArray(typeof(RegistrationErrorInfo)); throw new RegistrationException(Resource.FormatString("Reg_InvalidServicedComponents"), errorInfo); } }
private void WriteTypes(Stream s, Module m, Assembly asm, string strRuntimeVersion, RegistrationServices regServices, bool bGenerateTypeLib, int offset) { string fullName = null; string str2 = "{" + Marshal.GetTypeLibGuidForAssembly(asm).ToString().ToUpper() + "}"; foreach (Type type in m.GetTypes()) { if (regServices.TypeRequiresRegistration(type)) { string str3 = "{" + Marshal.GenerateGuidForType(type).ToString().ToUpper() + "}"; fullName = type.FullName; if (regServices.TypeRepresentsComType(type) || type.IsValueType) { this.WriteUTFChars(s, "<clrSurrogate" + Environment.NewLine, offset); this.WriteUTFChars(s, " clsid=\"" + str3 + "\"" + Environment.NewLine, offset); this.WriteUTFChars(s, " name=\"" + fullName + "\">" + Environment.NewLine, offset); this.WriteUTFChars(s, "</clrSurrogate>" + Environment.NewLine, offset); } else { string str4 = Marshal.GenerateProgIdForType(type); this.WriteUTFChars(s, "<clrClass" + Environment.NewLine, offset); this.WriteUTFChars(s, " clsid=\"" + str3 + "\"" + Environment.NewLine, offset); this.WriteUTFChars(s, " progid=\"" + str4 + "\"" + Environment.NewLine, offset); this.WriteUTFChars(s, " threadingModel=\"Both\"" + Environment.NewLine, offset); this.WriteUTFChars(s, " name=\"" + fullName + "\"" + Environment.NewLine, offset); this.WriteUTFChars(s, " runtimeVersion=\"" + strRuntimeVersion + "\">" + Environment.NewLine, offset); this.WriteUTFChars(s, "</clrClass>" + Environment.NewLine, offset); } } else if ((bGenerateTypeLib && type.IsInterface) && (type.IsPublic && !type.IsImport)) { string str5 = "{" + Marshal.GenerateGuidForType(type).ToString().ToUpper() + "}"; this.WriteUTFChars(s, "<comInterfaceExternalProxyStub" + Environment.NewLine, offset); this.WriteUTFChars(s, "iid=\"" + str5 + "\"" + Environment.NewLine, offset + 4); this.WriteUTFChars(s, "name=\"" + type.Name + "\"" + Environment.NewLine, offset + 4); this.WriteUTFChars(s, string.Concat(new object[] { "numMethods=\"", type.GetMethods().Length, "\"", Environment.NewLine }), offset + 4); this.WriteUTFChars(s, "proxyStubClsid32=\"{00020424-0000-0000-C000-000000000046}\"" + Environment.NewLine, offset + 4); this.WriteUTFChars(s, "tlbid=\"" + str2 + "\" />" + Environment.NewLine, offset + 4); } } }
static void WriteTypes(Stream s, Type[] aTypes, int offset) { RegistrationServices regServices = new RegistrationServices(); String name = null; Assembly asm = Assembly.GetExecutingAssembly(); string asmver = asm.ImageRuntimeVersion; foreach (Type t in aTypes) { // only registrable managed types will show up in the manifest file if (!regServices.TypeRequiresRegistration(t)) { throw Fx.AssertAndThrow("User defined types must be registrable"); } String strClsId = "{" + Marshal.GenerateGuidForType(t).ToString().ToUpperInvariant() + "}"; name = t.FullName; // this type is a com imported type or Record if (regServices.TypeRepresentsComType(t) || t.IsValueType) { WriteUTFChars(s, "<clrSurrogate" + Environment.NewLine, offset); // attribute clsid WriteUTFChars(s, " clsid=\"" + strClsId + "\"" + Environment.NewLine, offset); // attribute class WriteUTFChars(s, " name=\"" + name + "\"" + Environment.NewLine, offset); // clr version WriteUTFChars(s, " runtimeVersion=\"" + asmver + "\">" + Environment.NewLine, offset); WriteUTFChars(s, "</clrSurrogate>" + Environment.NewLine, offset); } } }
private static void WriteTypes(Stream s, Type[] aTypes, int offset) { RegistrationServices services = new RegistrationServices(); string fullName = null; string imageRuntimeVersion = Assembly.GetExecutingAssembly().ImageRuntimeVersion; foreach (Type type in aTypes) { if (!services.TypeRequiresRegistration(type)) { throw Fx.AssertAndThrow("User defined types must be registrable"); } string str3 = "{" + Marshal.GenerateGuidForType(type).ToString().ToUpperInvariant() + "}"; fullName = type.FullName; if (services.TypeRepresentsComType(type) || type.IsValueType) { WriteUTFChars(s, "<clrSurrogate" + Environment.NewLine, offset); WriteUTFChars(s, " clsid=\"" + str3 + "\"" + Environment.NewLine, offset); WriteUTFChars(s, " name=\"" + fullName + "\"" + Environment.NewLine, offset); WriteUTFChars(s, " runtimeVersion=\"" + imageRuntimeVersion + "\">" + Environment.NewLine, offset); WriteUTFChars(s, "</clrSurrogate>" + Environment.NewLine, offset); } } }
// write all the types need to register in module m private static void WriteTypes(Stream s, Module m, Assembly asm, String strRuntimeVersion, RegistrationServices regServices, bool bGenerateTypeLib, int offset) { String name = null; AssemblyName asmName = asm.GetName(); String strTlbId = "{" + Marshal.GetTypeLibGuidForAssembly(asm).ToString().ToUpper() + "}"; // element comClass or interface Type[] aTypes = m.GetTypes(); foreach (Type t in aTypes) { // only registrable managed types will show up in the manifest file if (regServices.TypeRequiresRegistration(t)) { String strClsId = "{" + Marshal.GenerateGuidForType(t).ToString().ToUpper() + "}"; name = t.FullName; // this type is a com imported type or Record if (regServices.TypeRepresentsComType(t) || t.IsValueType) { WriteUTFChars(s, "<clrSurrogate" + Environment.NewLine, offset); // attribute clsid WriteUTFChars(s, " clsid=\"" + strClsId + "\"" + Environment.NewLine, offset); // attribute class WriteUTFChars(s, " name=\"" + name + "\">" + Environment.NewLine, offset); WriteUTFChars(s, "</clrSurrogate>" + Environment.NewLine, offset); } else // this is a managed type { String strProgId = Marshal.GenerateProgIdForType(t); WriteUTFChars(s, "<clrClass" + Environment.NewLine, offset); // attribute clsid WriteUTFChars(s, " clsid=\"" + strClsId + "\"" + Environment.NewLine, offset); // attribute progid WriteUTFChars(s, " progid=\"" + strProgId + "\"" + Environment.NewLine, offset); // attribute threadingModel WriteUTFChars(s, " threadingModel=\"Both\"" + Environment.NewLine, offset); // attribute class WriteUTFChars(s, " name=\"" + name + "\"" + Environment.NewLine, offset); // attribute runtimeVersion WriteUTFChars(s, " runtimeVersion=\"" + strRuntimeVersion + "\">" + Environment.NewLine, offset); WriteUTFChars(s, "</clrClass>" + Environment.NewLine, offset); } } else { // public, non-imported from COM's interface need to be in the manifest if (bGenerateTypeLib && t.IsInterface && t.IsPublic && !t.IsImport) { String strIID = "{" + Marshal.GenerateGuidForType(t).ToString().ToUpper() + "}"; WriteUTFChars(s, "<comInterfaceExternalProxyStub" + Environment.NewLine, offset); WriteUTFChars(s, "iid=\"" + strIID + "\"" + Environment.NewLine, offset + 4); // Should t.FullName be used here? // Seems t.Name matches what regasm does. WriteUTFChars(s, "name=\"" + t.Name + "\"" + Environment.NewLine, offset + 4); WriteUTFChars(s, "numMethods=\"" + t.GetMethods().Length + "\"" + Environment.NewLine, offset + 4); // Seems regasm puts this guid for every interface. // Correct it if this is not true WriteUTFChars(s, "proxyStubClsid32=\"{00020424-0000-0000-C000-000000000046}\"" + Environment.NewLine, offset + 4); WriteUTFChars(s, "tlbid=\"" + strTlbId + "\" />" + Environment.NewLine, offset + 4); } } } }