コード例 #1
0
        /// <inheritdoc />
        public override bool Equals(object obj)
        {
            if (!IsInitialized)
            {
                return(this == obj);
            }

            IASValue other = obj as IASValue;

            return(other != null && externalizableValue.Equals(other.GetNativeValue(null)));
        }
コード例 #2
0
        private object UncachedToNative(IASValue asValue, Type nativeType)
        {
            ASTypeKind          kind         = asValue.Kind;
            ASClass             asClass      = asValue.Class;
            string              classAlias   = asClass != null ? asClass.ClassAlias : "";
            ASValueContentFlags contentFlags = asValue.ContentFlags;

            Type defaultNativeType = mappingTable.GetDefaultNativeType(kind, classAlias, contentFlags);

            // If the requested native type is not as precise as the default native type,
            // then use the default native type instead.  This rule is intended to avoid
            // the ambiguities that occur if we try to map to type "object" or some other
            // type that is too general.
            if (nativeType == null || nativeType.IsAssignableFrom(defaultNativeType))
            {
                nativeType = defaultNativeType;
            }

            // Quick short circuit if the desired type is a subtype of IASValue so mapping won't help.
            if (!typeof(IASValue).IsAssignableFrom(nativeType))
            {
                // Note: This will handle uninitialized values by returning null if no trivial
                //       conversion is possible without complete initialization.
                object value = asValue.GetNativeValue(nativeType);
                if (value != null)
                {
                    return(value);
                }

                // If the value isn't initialized then give up because we might have to do mapping.
                if (!asValue.IsInitialized)
                {
                    throw new ActionScriptException(String.Format(CultureInfo.CurrentCulture,
                                                                  "The ActionScript value cannot be mapped to type '{0}' because it is not completely initialized.",
                                                                  nativeType != null ? nativeType.FullName : "<default>"));
                }

                // Use mappers.
                // FIXME: This won't work if mapping ends up being recursive!
                ASSourceMappingDescriptor descriptor = new ASSourceMappingDescriptor(kind, classAlias, contentFlags, nativeType);
                IASSourceMapper           mapper     = mappingTable.GetASSourceMapper(descriptor);
                if (mapper != null)
                {
                    return(mapper.ToNative(this, asValue, nativeType));
                }
            }

            // Apply default handling for null references.
            if (asValue == ASNull.Value && !nativeType.IsValueType)
            {
                return(null);
            }

            // As a last resort, if we can assign the AS value to the original type requested then
            // do so.  We generally prefer mappings over returning IASValue instances, but if there
            // is no other choice...
            if (nativeType.IsInstanceOfType(asValue))
            {
                return(asValue);
            }

            // Give up!
            throw new ActionScriptException(String.Format(CultureInfo.CurrentCulture,
                                                          "Cannot find a suitable mapper for mapping an ActionScript value of kind '{0}' with class alias '{1}' to an instance of type '{2}'.",
                                                          asValue.Kind, classAlias, nativeType != null ? nativeType.FullName : "<default>"));
        }