コード例 #1
0
    public static System.Attribute ConvertPlatformAttribute(CustomAttributeData attribute, PlatformName platform)
    {
        var constructorArguments = new object [attribute.ConstructorArguments.Count];

        for (int i = 0; i < attribute.ConstructorArguments.Count; ++i)
        {
            constructorArguments [i] = attribute.ConstructorArguments [i].Value;
        }

        Func <string> createErrorMessage = () => {
            var b = new System.Text.StringBuilder(" Types { ");
            for (int i = 0; i < constructorArguments.Length; ++i)
            {
                b.Append(constructorArguments[i].GetType().ToString() + " ");
            }
            b.Append("}");
            return(b.ToString());
        };

        Func <string> unknownFormatError = () => $"Unknown format for old style availability attribute {attribute.AttributeType.FullName} {attribute.ConstructorArguments.Count} {createErrorMessage ()}";

        object []      ctorValues;
        System.Type [] ctorTypes;

        switch (attribute.ConstructorArguments.Count)
        {
        case 2:
            if (constructorArguments [0].GetType() == typeof(byte) &&
                constructorArguments [1].GetType() == typeof(byte))
            {
                ctorValues = new object [] { (byte)platform, (int)(byte)constructorArguments [0], (int)(byte)constructorArguments [1], (byte)0xff, null };
                ctorTypes  = new System.Type [] { AttributeFactory.PlatformEnum, typeof(int), typeof(int), AttributeFactory.PlatformArch, typeof(string) };
                break;
            }
            throw new NotImplementedException(unknownFormatError());

        case 3:
            if (constructorArguments [0].GetType() == typeof(byte) &&
                constructorArguments [1].GetType() == typeof(byte) &&
                constructorArguments [2].GetType() == typeof(byte))
            {
                ctorValues = new object [] { (byte)platform, (int)(byte)constructorArguments [0], (int)(byte)constructorArguments [1], (int)(byte)constructorArguments [2], (byte)0xff, null };
                ctorTypes  = new System.Type [] { AttributeFactory.PlatformEnum, typeof(int), typeof(int), typeof(int), AttributeFactory.PlatformArch, typeof(string) };
                break;
            }
            if (constructorArguments [0].GetType() == typeof(byte) &&
                constructorArguments [1].GetType() == typeof(byte) &&
                constructorArguments [2].GetType() == typeof(bool))
            {
                byte arch = (bool)constructorArguments [2] ? (byte)2 : (byte)0xff;
                ctorValues = new object [] { (byte)platform, (int)(byte)constructorArguments [0], (int)(byte)constructorArguments [1], arch, null };
                ctorTypes  = new System.Type [] { AttributeFactory.PlatformEnum, typeof(int), typeof(int), AttributeFactory.PlatformArch, typeof(string) };
                break;
            }
            throw new NotImplementedException(unknownFormatError());

        case 4:
            if (constructorArguments [0].GetType() == typeof(byte) &&
                constructorArguments [1].GetType() == typeof(byte) &&
                constructorArguments [2].GetType() == typeof(byte) &&
                constructorArguments [3].GetType() == typeof(bool))
            {
                byte arch = (bool)constructorArguments [3] ? (byte)2 : (byte)0xff;
                ctorValues = new object [] { (byte)platform, (int)(byte)constructorArguments [0], (int)(byte)constructorArguments [1], (int)(byte)constructorArguments [2], arch, null };
                ctorTypes  = new System.Type [] { AttributeFactory.PlatformEnum, typeof(int), typeof(int), typeof(int), AttributeFactory.PlatformArch, typeof(string) };
                break;
            }
            if (constructorArguments [0].GetType() == typeof(byte) &&
                constructorArguments [1].GetType() == typeof(byte) &&
                constructorArguments [2].GetType() == typeof(byte) &&
                constructorArguments [3].GetType() == typeof(byte) /* ObjCRuntime.PlatformArchitecture */)
            {
                ctorValues = new object [] { (byte)platform, (int)(byte)constructorArguments [0], (int)(byte)constructorArguments [1], (int)(byte)constructorArguments [2], constructorArguments [3], null };
                ctorTypes  = new System.Type [] { AttributeFactory.PlatformEnum, typeof(int), typeof(int), typeof(int), AttributeFactory.PlatformArch, typeof(string) };
                break;
            }

            throw new NotImplementedException(unknownFormatError());

        default:
            throw new NotImplementedException($"Unknown count {attribute.ConstructorArguments.Count} {createErrorMessage ()}");
        }

        return(AttributeFactory.CreateNewAttribute(AttributeFactory.IntroducedAttributeType, ctorTypes, ctorValues));
    }