예제 #1
0
        public static bool IsBlittable(this ITypeReference typeRef)
        {
            if (typeRef.IsPrimitive())
            {
                return(true);
            }

            if (typeRef.IsValueType)
            {
                var typeDef = typeRef.ResolvedType;
                if (string.Equals(typeDef.ToString(), "Microsoft.Cci.DummyNamespaceTypeDefinition"))
                {
                    throw new Exception($"Unable to find type def for {typeRef}. The assembly this type is defined in was not loaded");
                }

                foreach (var fieldInfo in typeDef.Fields)
                {
                    if (fieldInfo.IsStatic)
                    {
                        continue;
                    }

                    if (fieldInfo.IsMarshalledExplicitly || !fieldInfo.Type.IsBlittable())
                    {
                        return(false);
                    }
                }

                return(true);
            }

            var arrayType   = typeRef.ResolvedType as IArrayType;
            var elementType = arrayType?.ElementType;

            if (arrayType?.Rank == 1 && elementType.IsBlittable())
            {
                return(true);
            }

            return(false);
        }