I tried to change TargetRuntime (assemblyDefinition.Runtime =
TargetRuntime.NET_1_1;), it allowed to receive .NET 1.1 assembly. But
when I try to use this assembly I receive next message: "Resolved file
has a bad image, no metadata, or is otherwise inaccessible. Could not
load file or assembly 'MyAssembly.dll' or one of its dependencies.
File is corrupt. (Exception from HRESULT: 0x8013110E)".

Not I will try to use Mono.Cecil 0.9 and TargetRuntime.

Thanks,

Victor

Sources:
--------

using System;
using System.IO;
using Mono.Cecil;

namespace Internal2Public
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                string sourceFileName = args[0];
                string publicFileName = null;
                bool verbose false;

                Console.WriteLine("Start internal to public
replacement.");

                AssemblyDefinition assemblyDefinition =
AssemblyFactory.GetAssembly(sourceFileName);

                foreach (TypeDefinition typeDefinition in
assemblyDefinition.MainModule.Types)
                {
                    ProcessType(typeDefinition, verbose, false);
                }

                string publicAssemblyFileName = publicFileName;
                if (null == publicAssemblyFileName)
                {
                    publicAssemblyFileName =
string.Format(@"{0}\{1}.public{2}",
                        Path.GetDirectoryName(sourceFileName),
 
Path.GetFileNameWithoutExtension(sourceFileName),
                        Path.GetExtension(sourceFileName));
                }

                string publicAssemblyDir =
Path.GetDirectoryName(publicAssemblyFileName);
                if (!Directory.Exists(publicAssemblyDir))
                    Directory.CreateDirectory(publicAssemblyDir);

                AssemblyFactory.SaveAssembly(assemblyDefinition,
publicAssemblyFileName);

            } // try
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Environment.Exit(-1);
            }

            Console.WriteLine("Replacement completed successfully.");
            Environment.Exit(0);
        }

        private static void ProcessType(TypeDefinition typeDefinition,
bool verbose, bool isNested)
        {
            if (!isNested)
            {
                if (typeDefinition.IsNotPublic)
                {
                    typeDefinition.IsNotPublic = false;
                    typeDefinition.IsPublic = true;
                    if (verbose)
                        Console.WriteLine("The '{0}' type made
'public'.", typeDefinition.FullName);
                }
            }
            else
            {
                if (!typeDefinition.IsNestedPrivate && !
typeDefinition.IsNestedPublic)
                {
                    typeDefinition.IsNestedPrivate = false;
                    typeDefinition.IsNestedPublic = true;
                    if (verbose)
                        Console.WriteLine("The '{0}' type made 'nested
public'.", typeDefinition.FullName);
                }
            }

            bool first = true;

            foreach (FieldDefinition fieldDefinition in
typeDefinition.Fields)
            {
                if (fieldDefinition.IsAssembly)
                {
                    if (first)
                    {
                        if (verbose)
                            Console.WriteLine("  Fields:");
                        first = false;
                    }
                    fieldDefinition.IsAssembly = false;
                    fieldDefinition.IsPublic = true;
                    if (verbose)
                        Console.WriteLine("    The '{0}' field made
'public'.", fieldDefinition.Name);
                }
            }

            first = true;

            foreach (PropertyDefinition propertyDefinition in
typeDefinition.Properties)
            {
                if (null != propertyDefinition.SetMethod &&
propertyDefinition.SetMethod.IsAssembly)
                {
                    if (first)
                    {
                        if (verbose)
                            Console.WriteLine("  Properties:");
                        first = false;
                    }
                    propertyDefinition.SetMethod.IsAssembly = false;
                    propertyDefinition.SetMethod.IsPublic = true;
                    if (verbose)
                        Console.WriteLine("    The 'set' method of the
'{0}' property made 'public'.", propertyDefinition.Name);
                }

                if (null != propertyDefinition.GetMethod &&
propertyDefinition.GetMethod.IsAssembly)
                {
                    if (first)
                    {
                        if (verbose)
                            Console.WriteLine("  Properties:");
                        first = false;
                    }
                    propertyDefinition.GetMethod.IsAssembly = false;
                    propertyDefinition.GetMethod.IsPublic = true;
                    if (verbose)
                        Console.WriteLine("    The 'get' method of the
'{0}' property made 'public'.", propertyDefinition.Name);
                }
            }

            first = true;

            foreach (MethodDefinition methodDefinition in
typeDefinition.Methods)
            {
                if (methodDefinition.IsAssembly)
                {
                    if (first)
                    {
                        if (verbose)
                            Console.WriteLine("  Methods:");
                        first = false;
                    }
                    methodDefinition.IsAssembly = false;
                    methodDefinition.IsPublic = true;
                    if (verbose)
                        Console.WriteLine("    The '{0}' method made
'public'.", methodDefinition.Name);
                }
            }

            first = true;

            foreach (EventDefinition eventDefinition in
typeDefinition.Events)
            {
                if (eventDefinition.AddMethod.IsAssembly)
                {
                    if (first)
                    {
                        if (verbose)
                            Console.WriteLine("  Events:");
                        first = false;
                    }
                    eventDefinition.AddMethod.IsAssembly = false;
                    eventDefinition.AddMethod.IsPublic = true;
                    if (verbose)
                        Console.WriteLine("    The 'add' method of the
'{0}' event made 'public'.");
                }

                if (eventDefinition.RemoveMethod.IsAssembly)
                {
                    if (first)
                    {
                        if (verbose)
                            Console.WriteLine("  Events:");
                        first = false;
                    }
                    eventDefinition.RemoveMethod.IsAssembly = false;
                    eventDefinition.RemoveMethod.IsPublic = true;
                    if (verbose)
                        Console.WriteLine("    The 'add' method of the
'{0}' event made 'public'.");
                }
            }

            foreach (TypeDefinition nestedTypeDefinition in
typeDefinition.NestedTypes)
            {
                ProcessType(nestedTypeDefinition, verbose, true);
            }
        }

        private static void ShowHelp()
        {
            Console.WriteLine(
@"Internal2Public 1.0

Replaces the 'internal' access modifier by the 'public' access
modifier for types and type members.

    -s      Full name of source assembly.

    -p      Full name of result assembly (where all 'internal' made
'public').
            Defalut value is <source assembly name>.public.<source
assembly
            extention>.

    -v      Show changes.

    -h      Show this help.");
        }
    }
}


On Oct 10, 2:43 pm, Jb Evain <[email protected]> wrote:
> Hey,
>
> On Mon, Oct 10, 2011 at 1:24 PM, victor_k <[email protected]> wrote:
> > I have an .net 1.1 assembly. I want change some types visibility from
> > internal to public for testing. But when I make changes and save
> > assembly it changes target framework to .net 2.0. What can I do to
> > save in .net 1.1?
>
> You have to set the TargetRuntime to NET_1_1, and change the version
> of .net assembly referenced accordingly. We could help you more easily
> if you show some code.
>
> > I use Mono.Cecil 0.6 as last version which support .net 1.1
>
> Cecil 0.9 support .net 1.1 assemblies, but it needs to run on .net 2
> or bigger. So if you're only manipulating assemblies and executing
> them on another setup, you can use 0.9 without any issue.
>
> Best,
>
> Jb

-- 
--
mono-cecil

Reply via email to