Weaver.AssemblyWeaver.CloneMembers C# (CSharp) Method

CloneMembers() public method

public CloneMembers ( List clones ) : void
clones List
return void
        public void CloneMembers(List<MemberClone> clones)
        {
            foreach (var clone in clones)
            {
                var memberRef = CecilUtils.GetMember(TargetModule, clone.OriginMember);

                var propertyRef = memberRef as PropertyReference;
                if (propertyRef != null)
                {
                    var propertyDef = propertyRef.Resolve();

                    var clone1 = clone;
                    Copier.InitCopy = (origin, copy) =>
                    {
                        if (origin == propertyDef)
                            ((PropertyReference) copy).Name = clone1.Name;

                        if ( origin == propertyDef.GetMethod)
                            ((MethodReference) copy).Name = "get_" + clone1.Name;

                        if (origin == propertyDef.SetMethod)
                            ((MethodReference) copy).Name = "set_" + clone1.Name;
                    };

                    Copier.IsMemoizationEnabled = false;
                    var @ref = Copier.Copy<PropertyDefinition>(propertyDef);
                    Copier.IsMemoizationEnabled = true;
                    Copier.InitCopy = null;

                    if(clone.NoCustomAttributes)
                        @ref.CustomAttributes.Clear();

                    @ref.GetMethod.SetAccessModifier(clone.AccessModifier);
                    @ref.SetMethod.SetAccessModifier(clone.AccessModifier);

                    var declaringTypeDef = CecilUtils.GetMemberDef(TargetModule, propertyRef.DeclaringType.Resolve());
                    declaringTypeDef.Properties.Add(@ref);
                    continue;
                }

                throw new NotImplementedException();
            }

            Copier.Process();
        }

Usage Example

Example #1
0
        public void Compile(string dllPath, bool createApi)
        {
            FieldsToProperties(dllPath);

            var apiPath = (createApi)? Path.ChangeExtension(dllPath, "API.dll") : dllPath;

            var references = AssemblyWeaver.GetReferences(dllPath);
            references.Remove(Path.GetFileName(apiPath));

            var apiTypes = ReferenceCollector.CollectReferences(dllPath, RpcTypes);

            using (var apiGenPaths = new TemporaryAssemblyPaths())
            {
                var apiReferences = references.Where(s => s.EndsWith(".API.dll")).ToList();

                AssemblyWeaver weaver;

                if (createApi)
                {
                    //Create assembly with required types by the generated API assembly
                    weaver = new AssemblyWeaver();

                    weaver.AddReference(dllPath, "EngineManaged");
                    weaver.AddReference(dllPath, "EngineBindings");
                    weaver.AddReference(dllPath, "System");
                    weaver.AddReference(dllPath, "System.Core");
                    foreach (var apiReference in apiReferences)
                        weaver.AddReference(dllPath, Path.GetFileNameWithoutExtension(apiReference));

                    weaver.CopyTypes(dllPath, apiTypes);
                    weaver.Write(apiPath);
                }

                //API generated assembly
                var apiGenReferences = apiReferences.ToList();
                apiGenReferences.Add("EngineManaged.dll");
                apiGenReferences.Add(apiPath);
                DotNetCompiler.CompileIntoAssembly(apiGenPaths.DllPath, apiGenReferences, apiWriter.GeneratedFiles);

                //Add API generated assembly to API assembly
                weaver = new AssemblyWeaver(apiPath);
                weaver.CloneMembers(GenerationContext.MemberClones);
                weaver.AddMemberOptions(GenerationContext.MemberOptions);
                //Merge some generated files.
                weaver.MergeTypes(apiGenPaths.DllPath, GenerationContext.DataObjectsMap);
                //Add all the other generated types.
                weaver.CopyAssembly(apiGenPaths.DllPath);

                if (createApi)
                {
                    //Add attribute InternalsVisibleToAttribute
                    var dllName = Path.GetFileNameWithoutExtension(dllPath);
                    weaver.AddAttribute(typeof(InternalsVisibleToAttribute), new []{typeof(string)}, new object[]{dllName});
                }

                weaver.Write(apiPath);
            }

            if (!createApi)
                return;

            var typeFowarder = new TypeFowarder(dllPath);
            typeFowarder.FowardTypes(apiPath, apiTypes);
            typeFowarder.Write(dllPath);
        }