public override string ToString()
        {
            var propStringBuilder = new StringBuilder();

            foreach (var a in AttributesForProperties())
            {
                propStringBuilder.AppendLine(a.ToString());
            }

            var supertype       = string.Empty;
            var baseConstructor = string.Empty;

            if (SubtypeOf.Any())
            {
                supertype       = $" : {string.Join(",",SubtypeOf)}";
                baseConstructor = $" : base({(BaseConstructorParameters())})";
            }

            var assignBuilder = new StringBuilder();

            if (Name == "IfcRoot")
            {
                assignBuilder.AppendLine($"\t\t\tGlobalId = new IfcGloballyUniqueId(\"{Guid.NewGuid().ToString()}\");");
            }
            else
            {
                foreach (var a in AttributesForUseInConstructors())
                {
                    assignBuilder.AppendLine($"\t\t\t{a.TypeInfo.Name} = {a.TypeInfo.ParameterName};");
                }
            }


            var classStr =
                $@"
	/// <summary>
	/// <see href=""http://www.buildingsmart-tech.org/ifc/IFC4/final/html/link/{Name.ToLower()}.htm""/>
	/// </summary>
	public {Modifier()} partial class {Name}{supertype}
	{{
{propStringBuilder.ToString()}
		public {Name}({ConstructorParameters()}){baseConstructor}
		{{
{assignBuilder.ToString()}
		}}
	}}
";

            return(classStr);
        }
Пример #2
0
        private static Type maketywrapper(LocationInfo sourcelocation, Type source, Type target)
        {
            BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly;

            SubtypeOf sts = (SubtypeOf)Attribute.GetCustomAttribute(target, typeof(SubtypeOf));

            Type[] explicitSts = null;
            if (sts != null)
            {
                explicitSts = sts.Subtypes;
            }
            else
            {
                explicitSts = Type.EmptyTypes;
            }

            Dictionary <string, PropertyInfo> srcPropMap, tgtPropMap;
            Dictionary <string, MethodInfo>   srcMethMap, tgtMethMap;

            MakeMemberMaps(source, flags, sourcelocation, out srcPropMap, out srcMethMap);
            MakeMemberMaps(target, flags, sourcelocation, out tgtPropMap, out tgtMethMap);

            foreach (string k in tgtPropMap.Keys)
            {
                if (!srcPropMap.ContainsKey(k))
                {
                    throw new CastException(sourcelocation);
                }
                if (tgtPropMap[k].CanRead && !srcPropMap[k].CanRead)
                {
                    throw new CastException(sourcelocation);
                }
                if (tgtPropMap[k].CanWrite && !srcPropMap[k].CanWrite)
                {
                    throw new CastException(sourcelocation);
                }
            }

            foreach (string k in tgtMethMap.Keys)
            {
                if (!srcMethMap.ContainsKey(k))
                {
                    throw new CastException(sourcelocation);
                }
            }

            //all compatibility checks done
            //time to turn the emit crank
            TypeBuilder tb = mb.DefineType(source.Name + "to" + target.Name + "wrapper", TypeAttributes.Public);

            tb.AddInterfaceImplementation(target);
            var thatf = tb.DefineField("that", source, FieldAttributes.Private);
            var locf  = tb.DefineField("loc", typeof(LocationInfo), FieldAttributes.Private);

            MakeConstructor(source, tb, thatf, locf);

            Dictionary <string, PropertyBuilder> properties = new Dictionary <string, PropertyBuilder>();
            Dictionary <string, MethodBuilder>   methods    = new Dictionary <string, MethodBuilder>();

            foreach (string k in srcMethMap.Keys)
            {
                MethodInfo    srcInfo = srcMethMap[k];
                MethodBuilder generated;
                if (tgtMethMap.ContainsKey(k))
                {
                    MethodInfo tgtInfo = tgtMethMap[k];
                    generated = MakeMethodWrapper(tb, thatf, locf, k, srcInfo,
                                                  tgtInfo.GetParameters().Select(param => param.ParameterType).ToArray(),
                                                  tgtInfo.ReturnType, tgtInfo, sourcelocation);
                }
                else
                {
                    generated = MakeMethodWrapper(tb, thatf, locf, k, srcInfo,
                                                  srcInfo.GetParameters().Select(param => param.ParameterType).ToArray(),
                                                  srcInfo.ReturnType, null, sourcelocation);
                }
                methods.Add(k, generated);
            }

            foreach (Type mbSt in explicitSts)
            {
                MakeExplicitImplementation(tb, mbSt, flags, properties, methods);
                tb.AddInterfaceImplementation(mbSt);
            }

            Type wrapper = tb.CreateType();

            return(wrapper);
        }