public void TestGenerateSpecialNameRepId()
        {
            string name = "TestUnionGenForTypeCodeType3";
            string typeName = "Ch.Elca.Iiop.Tests." + name;
            string repId = "IDL:Ch/Elca/Iiop/Tests/Special_TestUnionGenForTypeCodeType3:1.0";

            UnionSwitchCase s1 = new UnionSwitchCase((int)0, "val_0", new LongTC());
            UnionSwitchCase s2 = new UnionSwitchCase((int)1, "val_1", new FloatTC());
            TypeCodeImpl discrTC = new LongTC();
            UnionTC tc = new UnionTC(repId, name, discrTC, 0,
                                     new UnionSwitchCase[] { s1, s2 });
            Type res = m_gen.CreateOrGetType(typeName, tc);
            Assert.NotNull(res);
            Assert.AreEqual(typeName, res.FullName, "type name");
            Assert.AreEqual(repId, Repository.GetRepositoryID(res), "rep id");
        }
        public object MapToIdlUnion(Type clsType) {
            UnionTC result = new UnionTC();            
            RegisterCreatedTypeCodeForType(clsType, AttributeExtCollection.EmptyCollection,
                                           result);
            
            // first get discriminator type
            FieldInfo discriminator = clsType.GetField(UnionGenerationHelper.DISCR_FIELD_NAME, 
                                                       BindingFlags.Instance | 
                                                       BindingFlags.DeclaredOnly |
                                                       BindingFlags.NonPublic);
            omg.org.CORBA.TypeCode discrTypeCode = 
                CreateOrGetTypeCodeForType(discriminator.FieldType, 
                                           ReflectionHelper.GetCustomAttriutesForField(discriminator,
                                                                                       true));            
            // get the methods used for typecode creation
            MethodInfo getCoveredDiscrMethod = clsType.GetMethod(UnionGenerationHelper.GET_COVERED_DISCR_VALUES,
                                                                 BindingFlags.Static | BindingFlags.NonPublic |
                                                                 BindingFlags.DeclaredOnly);
            MethodInfo getDefaultFieldMethod = clsType.GetMethod(UnionGenerationHelper.GET_DEFAULT_FIELD,
                                                                 BindingFlags.Static | BindingFlags.NonPublic |
                                                                 BindingFlags.DeclaredOnly);
            MethodInfo getFieldForDiscrVal = clsType.GetMethod(UnionGenerationHelper.GET_FIELD_FOR_DISCR_METHOD,
                                                               BindingFlags.Static | BindingFlags.NonPublic |
                                                               BindingFlags.DeclaredOnly);

            // get all discriminator values used in switch-cases
            object[] coveredDiscrs = (object[])getCoveredDiscrMethod.Invoke(null, new object[0]);
            if (coveredDiscrs == null) {
                throw new INTERNAL(898, CompletionStatus.Completed_MayBe);
            }
                        
            FieldInfo defaultField = (FieldInfo)getDefaultFieldMethod.Invoke(null, new object[0]);
            
            UnionSwitchCase[] cases = null;
            int defaultCaseNumber = -1; // no default case
            if (defaultField != null) {
                cases = new UnionSwitchCase[coveredDiscrs.Length + 1];
                omg.org.CORBA.TypeCode elemType = 
                    CreateOrGetTypeCodeForType(defaultField.FieldType, 
                                               ReflectionHelper.GetCustomAttriutesForField(defaultField, 
                                                                                           true));
                // create a default value of type discriminiator type, because of possible discriminator types, this 
                // is possible with Activator.CreateInstance ...
                object dummyValue = null;
                try {
                    dummyValue = Activator.CreateInstance(discriminator.FieldType);
                } catch (Exception) {
                    throw new MARSHAL(881, CompletionStatus.Completed_MayBe);
                }
                cases[coveredDiscrs.Length] = new UnionSwitchCase(dummyValue, defaultField.Name.Substring(2),
                                                                  elemType);
                defaultCaseNumber = coveredDiscrs.Length;
            } else {
                cases = new UnionSwitchCase[coveredDiscrs.Length];
            }
            
            // add a UnionSwitchCase to typecode for every discriminator value used
            for (int i = 0; i < coveredDiscrs.Length; i++) {
                FieldInfo caseField = (FieldInfo)getFieldForDiscrVal.Invoke(null, new object[] { coveredDiscrs[i] });
                if (caseField == null) {
                    throw new INTERNAL(898, CompletionStatus.Completed_MayBe);
                }
                omg.org.CORBA.TypeCode elemType = 
                    CreateOrGetTypeCodeForType(caseField.FieldType, 
                                               ReflectionHelper.GetCustomAttriutesForField(caseField, true));
                // extract name of element field: strip m_
                UnionSwitchCase switchCase = new UnionSwitchCase(coveredDiscrs[i], caseField.Name.Substring(2),
                                                                 elemType);
                cases[i] = switchCase;
            }                                                                                                
            result.Initalize(Repository.GetRepositoryID(clsType),            
                             IdlNaming.ReverseIdlToClsNameMapping(clsType.Name),
                             discrTypeCode, defaultCaseNumber, cases);
            return result;
        }
        public void TestGenerate()
        {
            string name = "TestUnionGenForTypeCodeType";
            string typeName = "Ch.Elca.Iiop.Tests." + name;
            string repId = "IDL:Ch/Elca/Iiop/Tests/TestUnionGenForTypeCodeType:1.0";

            UnionSwitchCase s1 = new UnionSwitchCase((int)0, "val_0", new LongTC());
            UnionSwitchCase s2 = new UnionSwitchCase((int)1, "val_1", new FloatTC());
            TypeCodeImpl discrTC = new LongTC();
            UnionTC tc = new UnionTC(repId, name, discrTC, 0,
                                     new UnionSwitchCase[] { s1, s2 });
            Type res = m_gen.CreateOrGetType(typeName, tc);
            Assert.NotNull(res);
            Assert.AreEqual(typeName, res.FullName, "type name");
            Assert.AreEqual(repId, Repository.GetRepositoryID(res), "rep id");

            MethodInfo getFieldForDiscrMethod =
                res.GetMethod(UnionGenerationHelper.GET_FIELD_FOR_DISCR_METHOD, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
            Assert.NotNull(getFieldForDiscrMethod, "get field for Discr method");
            FieldInfo fieldForDiscr1 = (FieldInfo)
                getFieldForDiscrMethod.Invoke(null, new object[] { s1.DiscriminatorValue });
            FieldInfo fieldForDiscr2 = (FieldInfo)
                getFieldForDiscrMethod.Invoke(null, new object[] { s2.DiscriminatorValue });
            Assert.NotNull(fieldForDiscr1, "fieldForDiscr1");
            Assert.NotNull(fieldForDiscr2, "fieldForDiscr2");
            Assert.AreEqual(((TypeCodeImpl)s1.ElementType).GetClsForTypeCode(),
                                   fieldForDiscr1.FieldType, "fieldForDiscr1 Type");
            Assert.AreEqual(
                                   ((TypeCodeImpl)s2.ElementType).GetClsForTypeCode(),
                                   fieldForDiscr2.FieldType, "fieldForDiscr2 Type");
            PropertyInfo discrProperty = res.GetProperty(UnionGenerationHelper.DISCR_PROPERTY_NAME,
                                                         BindingFlags.Public | BindingFlags.Instance);
            Assert.NotNull(discrProperty, "discr property");
            Assert.AreEqual(discrTC.GetClsForTypeCode(),
                                   discrProperty.PropertyType, "discr property type");
            Assert.IsTrue(res.IsSerializable, "Serializable");
        }