当前位置: 首页>>代码示例>>C#>>正文


C# Collection.AddI方法代码示例

本文整理汇总了C#中Collection.AddI方法的典型用法代码示例。如果您正苦于以下问题:C# Collection.AddI方法的具体用法?C# Collection.AddI怎么用?C# Collection.AddI使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Collection的用法示例。


在下文中一共展示了Collection.AddI方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: HandleDictionary

        private void HandleDictionary(
            Collection<VariableDefinition> variables,
            Collection<ExceptionHandler> exceptionHandlers,
            Collection<Instruction> instructions,
            TypeReference dictRef,
            Action<Collection<Instruction>> load,
            Action<Collection<Instruction>> store)
        {
            string fullName = dictRef.FullName;

            var git = (dictRef as GenericInstanceType);
            if (null == git || !git.HasGenericArguments || 2 != git.GenericArguments.Count)
                throw new Exception(String.Format("Unhandled case for type {0}.", fullName));

            MethodReference enumeratorMethod = this.GetTypeMethod(dictRef, "GetEnumerator");
            MethodReference comparerMethod = this.GetTypeMethod(dictRef, "get_Comparer");
            MethodReference addMethod = this.GetTypeMethod(dictRef, "Add");

            // Do not merge this variable with enumeratorMethod (it get's modified).
            var methodReference = this.GetTypeMethod(dictRef, "GetEnumerator");
            var getCount = this.GetTypeMethod(dictRef, "get_Count");
            var genericEnumerator = methodReference.ReturnType as GenericInstanceType;
            if (null != genericEnumerator)
            {
                genericEnumerator.GenericArguments.Clear();

                var baseRef = dictRef as GenericInstanceType;
                foreach (var arg in baseRef.GenericArguments)
                    genericEnumerator.GenericArguments.Add(arg);
            }

            MethodReference getCurrent = this.GetTypeMethod(genericEnumerator, "get_Current");
            MethodReference moveNext = this.GetTypeMethod(genericEnumerator, "MoveNext");

            MethodReference dispose;
            if (!this.ModuleDefinition.TryGetMethodReference(typeof (IDisposable), "Dispose", out dispose))
                throw new Exception(String.Format("Unable to get IDisposable.Dispose() for type {0}.", fullName));

            var intType = this.ModuleDefinition.Import(this.ModuleDefinition.TypeSystem.Int32).Resolve();
            var dictOfObjConstructor = this.GetGenericTypeConstructorMethod(dictRef, new []
                {
                    new ParameterDefinition(intType),
                    new ParameterDefinition(comparerMethod.ReturnType)
                });

            var typeReference = getCurrent.ReturnType.GetElementType();
            var genericDict = dictRef as GenericInstanceType;
            var genericKVP = typeReference.MakeGenericType(genericDict.GenericArguments.ToArray());

            MethodReference getKey = this.GetTypeMethod(genericKVP, "get_Key");
            MethodReference getValue = this.GetTypeMethod(genericKVP, "get_Value");

            MethodDefinition keyDeepCopy = null;
            var keyDef = genericDict.GenericArguments[0] as TypeDefinition;
            if (null != keyDef)
                keyDef.TryGetMethod(this._deepCopyMethodName, out keyDeepCopy);

            MethodDefinition valueDeepCopy = null;
            var valueDef = genericDict.GenericArguments[1] as TypeDefinition;
            if (null != valueDef)
                valueDef.TryGetMethod(this._deepCopyMethodName, out valueDeepCopy);

            var newDict = variables.AddV(dictRef);
            var enumerator = variables.AddV(genericEnumerator);
            var kvp = variables.AddV(genericKVP);
            VariableDefinition kvpValue = null;
            if (null != valueDef)
                kvpValue = variables.AddV(valueDef);

            var IL_006f = Instruction.Create(OpCodes.Ldloc_0);
            instructions.AddI(OpCodes.Ldnull);
            instructions.AddI(OpCodes.Stloc, newDict);
            instructions.AddI(OpCodes.Ldnull);
            instructions.AddI(OpCodes.Ldarg_0);
            load(instructions);
            instructions.AddI(OpCodes.Ceq);
            instructions.AddI(OpCodes.Brtrue_S, IL_006f);

            instructions.AddI(OpCodes.Ldarg_0);
            load(instructions);
            instructions.AddI(OpCodes.Callvirt, getCount);

            instructions.AddI(OpCodes.Ldarg_0);
            load(instructions);
            instructions.AddI(OpCodes.Callvirt, comparerMethod);
            instructions.AddI(OpCodes.Newobj, dictOfObjConstructor);
            instructions.AddI(OpCodes.Stloc, newDict);
            instructions.AddI(OpCodes.Ldarg_0);
            load(instructions);
            instructions.AddI(OpCodes.Callvirt, enumeratorMethod);
            instructions.AddI(OpCodes.Stloc_S, enumerator);

            // try
            var IL_004f = Instruction.Create(OpCodes.Ldloca_S, enumerator);
            var tryStart = Instruction.Create(OpCodes.Br_S, IL_004f);
            instructions.Add(tryStart);

            // loop start
            var loopStart = Instruction.Create(OpCodes.Ldloca_S, enumerator);
            instructions.Add(loopStart);
//.........这里部分代码省略.........
开发者ID:JesseBuesking,项目名称:BB.DeepCopy,代码行数:101,代码来源:ModuleWeaver.cs

示例2: HandleListOfObjects

        private void HandleListOfObjects(Collection<VariableDefinition> variables,
            Collection<ExceptionHandler> exceptionHandlers, Collection<Instruction> instructions,
            TypeReference listRef, Action<Collection<Instruction>> load, Action<Collection<Instruction>> store,
            TypeReference listObjectsType, MethodReference getCount, MethodReference getItem,
            MethodReference addMethod)
        {
            var typeDefinition = listObjectsType as TypeDefinition;
            if (null == typeDefinition)
                throw new Exception(
                    String.Format("List object type {0} is not a TypeDefinition.", listObjectsType.FullName));

            MethodDefinition deepCopy;
            if (!typeDefinition.TryGetMethod(this._deepCopyMethodName, out deepCopy))
                throw new Exception(
                    String.Format("Sub-type {0} does not implement DeepCopy.", typeDefinition.FullName));

            var intType = this.ModuleDefinition.Import(this.ModuleDefinition.TypeSystem.Int32).Resolve();
            var listOfObjConstructor = this.GetGenericTypeConstructorMethod(listRef, new[]
                {
                    new ParameterDefinition(intType)
                });

            var newList = variables.AddV(listRef);
            var listObject = variables.AddV(listObjectsType);
            var count = variables.AddV(intType);
            var currentIndex = variables.AddV(intType);

            var loadNullAndRet = Instruction.Create(OpCodes.Ldloc_0);
            instructions.AddI(OpCodes.Ldnull);
            instructions.AddI(OpCodes.Stloc, newList);
            instructions.AddI(OpCodes.Ldnull);
            instructions.AddI(OpCodes.Ldarg_0);
            load(instructions);
            instructions.AddI(OpCodes.Ceq);
            instructions.AddI(OpCodes.Brtrue_S, loadNullAndRet);

            var forLoopCondition = Instruction.Create(OpCodes.Ldloc, currentIndex);
            instructions.AddI(OpCodes.Ldarg_0);
            load(instructions);
            instructions.AddI(OpCodes.Callvirt, getCount);
            instructions.AddI(OpCodes.Stloc, count);
            instructions.AddI(OpCodes.Ldloc, count);
            instructions.AddI(OpCodes.Newobj, listOfObjConstructor);
            instructions.AddI(OpCodes.Stloc, newList);
            instructions.AddI(OpCodes.Ldc_I4_0);
            instructions.AddI(OpCodes.Stloc, currentIndex);
            instructions.AddI(OpCodes.Br_S, forLoopCondition);

            // loop start
            var storeNull = Instruction.Create(OpCodes.Ldnull);
            var loopStart = Instruction.Create(OpCodes.Ldarg_0);
            instructions.Add(loopStart);
            load(instructions);
            instructions.AddI(OpCodes.Ldloc, currentIndex);
            instructions.AddI(OpCodes.Callvirt, getItem);
            instructions.AddI(OpCodes.Stloc, listObject);
            instructions.AddI(OpCodes.Ldloc, newList);
            instructions.AddI(OpCodes.Ldloc, listObject);
            instructions.AddI(OpCodes.Brfalse_S, storeNull);

            var add = Instruction.Create(OpCodes.Callvirt, addMethod);
            instructions.AddI(OpCodes.Ldloc, listObject);
            instructions.AddI(OpCodes.Callvirt, deepCopy);
            instructions.AddI(OpCodes.Br_S, add);

            instructions.Add(storeNull);

            instructions.Add(add);

            // loop increment
            instructions.AddI(OpCodes.Ldloc, currentIndex);
            instructions.AddI(OpCodes.Ldc_I4_1);
            instructions.AddI(OpCodes.Add);
            instructions.AddI(OpCodes.Stloc, currentIndex);

            // check if condition
            instructions.Add(forLoopCondition);
            instructions.AddI(OpCodes.Ldloc, count);
            instructions.AddI(OpCodes.Clt);
            instructions.AddI(OpCodes.Brtrue_S, loopStart);
            // end loop

            instructions.Add(loadNullAndRet);
            instructions.AddI(OpCodes.Ldloc, newList);
            store(instructions);
        }
开发者ID:JesseBuesking,项目名称:BB.DeepCopy,代码行数:86,代码来源:ModuleWeaver.cs

示例3: HandleListOfPrimitives

        private void HandleListOfPrimitives(Collection<VariableDefinition> variables,
            Collection<ExceptionHandler> exceptionHandlers, Collection<Instruction> instructions,
            TypeReference listRef, Action<Collection<Instruction>> load, Action<Collection<Instruction>> store,
            MethodReference getCount, MethodReference getItem, MethodReference addMethod)
        {
            var intType = this.ModuleDefinition.Import(this.ModuleDefinition.TypeSystem.Int32).Resolve();
            var listOfObjConstructor = this.GetGenericTypeConstructorMethod(listRef, new[]
                {
                    new ParameterDefinition(intType)
                });

            var newList = variables.AddV(listRef);
            var count = variables.AddV(intType);
            var currentIndex = variables.AddV(intType);

            var loadNullAndRet = Instruction.Create(OpCodes.Ldloc_0);
            instructions.AddI(OpCodes.Ldnull);
            instructions.AddI(OpCodes.Stloc, newList);
            instructions.AddI(OpCodes.Ldnull);
            instructions.AddI(OpCodes.Ldarg_0);
            load(instructions);
            instructions.AddI(OpCodes.Ceq);
            instructions.AddI(OpCodes.Brtrue_S, loadNullAndRet);

            var forLoopCondition = Instruction.Create(OpCodes.Ldloc, currentIndex);
            instructions.AddI(OpCodes.Ldarg_0);
            load(instructions);
            instructions.AddI(OpCodes.Callvirt, getCount);
            instructions.AddI(OpCodes.Stloc, count);
            instructions.AddI(OpCodes.Ldloc, count);
            instructions.AddI(OpCodes.Newobj, listOfObjConstructor);
            instructions.AddI(OpCodes.Stloc, newList);
            instructions.AddI(OpCodes.Ldc_I4_0);
            instructions.AddI(OpCodes.Stloc, currentIndex);
            instructions.AddI(OpCodes.Br_S, forLoopCondition);

            // loop start
            var loopStart = Instruction.Create(OpCodes.Ldloc, newList);
            instructions.Add(loopStart);
            instructions.AddI(OpCodes.Ldarg_0);
            load(instructions);
            instructions.AddI(OpCodes.Ldloc, currentIndex);
            instructions.AddI(OpCodes.Callvirt, getItem);
            instructions.AddI(OpCodes.Callvirt, addMethod);

            // loop increment
            instructions.AddI(OpCodes.Ldloc, currentIndex);
            instructions.AddI(OpCodes.Ldc_I4_1);
            instructions.AddI(OpCodes.Add);
            instructions.AddI(OpCodes.Stloc, currentIndex);

            // check if condition
            instructions.Add(forLoopCondition);
            instructions.AddI(OpCodes.Ldloc, count);
            instructions.AddI(OpCodes.Clt);
            instructions.AddI(OpCodes.Brtrue_S, loopStart);
            // end loop

            instructions.Add(loadNullAndRet);
            instructions.AddI(OpCodes.Ldloc, newList);
            store(instructions);
        }
开发者ID:JesseBuesking,项目名称:BB.DeepCopy,代码行数:62,代码来源:ModuleWeaver.cs

示例4: HandleArray

        private void HandleArray(
            Collection<VariableDefinition> variables,
            Collection<ExceptionHandler> exceptionHandlers,
            Collection<Instruction> instructions,
            TypeReference arrayRef,
            Action<Collection<Instruction>> load,
            Action<Collection<Instruction>> store)
        {
            ArrayType arrayType = (ArrayType) arrayRef;
            TypeReference elementType = arrayType.ElementType;

            if (elementType.IsPrimitiveObject())
            {
                var newArray = variables.AddV(arrayRef);
                var currentIndex = variables.AddV(this.ModuleDefinition.TypeSystem.Int32); // TODO replace
                var length = variables.AddV(this.ModuleDefinition.TypeSystem.Int32); // TODO replace

                var loadMainObj = Instruction.Create(OpCodes.Ldloc_0);
                instructions.AddI(OpCodes.Ldnull);
                instructions.AddI(OpCodes.Stloc, newArray);
                instructions.AddI(OpCodes.Ldnull);
                instructions.AddI(OpCodes.Ldarg_0);
                load(instructions);
                instructions.AddI(OpCodes.Ceq);
                instructions.AddI(OpCodes.Brtrue_S, loadMainObj);

                var IL_0059 = Instruction.Create(OpCodes.Ldloc, currentIndex);
                instructions.AddI(OpCodes.Ldarg_0);
                load(instructions);
                instructions.AddI(OpCodes.Ldlen);
                instructions.AddI(OpCodes.Conv_I4); // TODO replace
                instructions.AddI(OpCodes.Stloc, length);
                instructions.AddI(OpCodes.Ldloc, length);
                instructions.AddI(OpCodes.Newarr, elementType);
                instructions.AddI(OpCodes.Stloc, newArray);
                instructions.AddI(OpCodes.Ldc_I4_0); // TODO replace
                instructions.AddI(OpCodes.Stloc, currentIndex);
                instructions.AddI(OpCodes.Br_S, IL_0059);

                // loop start
                var IL_0025 = Instruction.Create(OpCodes.Ldloc, newArray);
                instructions.Add(IL_0025);
                instructions.AddI(OpCodes.Ldloc, currentIndex);
                instructions.AddI(OpCodes.Ldarg_0);
                load(instructions);
                instructions.AddI(OpCodes.Ldloc, currentIndex);

                // Change for each primitive.
                Type type = Type.GetType(elementType.FullName);
                if (typeof (System.Int16) == type)
                {
                    instructions.AddI(OpCodes.Ldelem_I2);
                    instructions.AddI(OpCodes.Stelem_I2);
                }
                else if (typeof (System.Int32) == type)
                {
                    instructions.AddI(OpCodes.Ldelem_I4);
                    instructions.AddI(OpCodes.Stelem_I4);
                }
                else if (typeof (System.Int64) == type)
                {
                    instructions.AddI(OpCodes.Ldelem_I8);
                    instructions.AddI(OpCodes.Stelem_I8);
                }
                else if (typeof (float) == type)
                {
                    instructions.AddI(OpCodes.Ldelem_R4);
                    instructions.AddI(OpCodes.Stelem_R4);
                }
                else if (typeof (double) == type)
                {
                    instructions.AddI(OpCodes.Ldelem_R8);
                    instructions.AddI(OpCodes.Stelem_R8);
                }
                else if (typeof (System.UInt16) == type)
                {
                    instructions.AddI(OpCodes.Ldelem_U2);
                    instructions.AddI(OpCodes.Stelem_I4);
                }
                else if (typeof (System.UInt32) == type)
                {
                    instructions.AddI(OpCodes.Ldelem_U4);
                    instructions.AddI(OpCodes.Stelem_I4);
                }

                instructions.AddI(OpCodes.Ldloc, currentIndex);
                instructions.AddI(OpCodes.Ldc_I4_1); // TODO replace
                instructions.AddI(OpCodes.Add);
                instructions.AddI(OpCodes.Stloc, currentIndex);

                // Increment the index.
                instructions.Add(IL_0059);
                instructions.AddI(OpCodes.Ldloc, length);
                instructions.AddI(OpCodes.Clt);
                instructions.AddI(OpCodes.Brtrue_S, IL_0025);
                // end loop

                instructions.Add(loadMainObj);
                instructions.AddI(OpCodes.Ldloc, newArray);
                store(instructions);
//.........这里部分代码省略.........
开发者ID:JesseBuesking,项目名称:BB.DeepCopy,代码行数:101,代码来源:ModuleWeaver.cs

示例5: SafeCallDeepCopy

        private void SafeCallDeepCopy(
            Collection<VariableDefinition> variables,
            Collection<ExceptionHandler> exceptionHandlers,
            Collection<Instruction> instructions,
            TypeDefinition typeDefinition,
            Action<Collection<Instruction>> load,
            Action<Collection<Instruction>> store)
        {
            MethodDefinition deepCopy;
            if (!typeDefinition.TryGetMethod(this._deepCopyMethodName, out deepCopy))
                throw new Exception(
                    String.Format("Sub-type {0} does not implement DeepCopy.", typeDefinition.FullName));

            var var0 = variables.AddV(typeDefinition);

            // Load the object, and check to see if it's null.
            instructions.AddI(OpCodes.Nop);
            instructions.AddI(OpCodes.Ldarg_0);
            load(instructions);
            instructions.AddI(OpCodes.Stloc, var0);
            instructions.AddI(OpCodes.Ldloc_0);
            instructions.AddI(OpCodes.Ldloc, var0);

            var loadNull = Instruction.Create(OpCodes.Ldnull);
            instructions.AddI(OpCodes.Brfalse_S, loadNull);

            instructions.AddI(OpCodes.Ldloc, var0);
            instructions.AddI(OpCodes.Callvirt, deepCopy);
            var noOp = Instruction.Create(OpCodes.Nop);
            instructions.AddI(OpCodes.Br_S, noOp);

            instructions.Add(loadNull);

            instructions.Add(noOp);
            store(instructions);
        }
开发者ID:JesseBuesking,项目名称:BB.DeepCopy,代码行数:36,代码来源:ModuleWeaver.cs

示例6: CopyPrimitive

 private void CopyPrimitive(
     Collection<VariableDefinition> variables,
     Collection<ExceptionHandler> exceptionHandlers,
     Collection<Instruction> instructions,
     Action<Collection<Instruction>> load,
     Action<Collection<Instruction>> store)
 {
     instructions.AddI(OpCodes.Ldloc_0);
     instructions.AddI(OpCodes.Ldarg_0);
     load(instructions);
     store(instructions);
 }
开发者ID:JesseBuesking,项目名称:BB.DeepCopy,代码行数:12,代码来源:ModuleWeaver.cs


注:本文中的Collection.AddI方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。