Example #1
0
        private void MarshalValueClassProperty(Property property, string marshalVar)
        {
            var fieldRef = string.Format("{0}.{1}", Context.Parameter.Name,
                                         property.Name);

            var marshalCtx = new MarshalContext(Context.Driver)
                                 {
                                     ArgName = fieldRef,
                                     ParameterIndex = Context.ParameterIndex++,
                                     MarshalVarPrefix = Context.MarshalVarPrefix
                                 };

            var marshal = new CLIMarshalManagedToNativePrinter(marshalCtx);
            property.Visit(marshal);

            Context.ParameterIndex = marshalCtx.ParameterIndex;

            if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
                Context.SupportBefore.Write(marshal.Context.SupportBefore);

            Type type;
            Class @class;
            var isRef = property.Type.IsPointerTo(out type) &&
                !(type.TryGetClass(out @class) && @class.IsValueType) &&
                !type.IsPrimitiveType();

            if (isRef)
            {
                Context.SupportBefore.WriteLine("if ({0} != nullptr)", fieldRef);
                Context.SupportBefore.PushIndent();
            }

            Context.SupportBefore.WriteLine("{0}.{1} = {2};", marshalVar,
                property.Field.OriginalName, marshal.Context.Return);

            if (isRef)
                Context.SupportBefore.PopIndent();
        }