コード例 #1
0
        private void GenerateSetKeyValueDirect()
        {
            var method = new MethodDefinition("SetKeyValueDirect", MethodAttr, _handler.VoidType);

            method.Overrides.Add(_handler.SetKeyValueDirect);
            method.Parameters.Add(new ParameterDefinition("obj", ParameterAttributes.None, _handler.ObjectType));
            method.Parameters.Add(new ParameterDefinition("key", ParameterAttributes.None, _handler.ObjectType));
            var processor = new IlBuilder(method.Body);

            if (_info.KeyMembers.Length == 1)
            {
                var h = _info.KeyMembers[0];
                processor.LoadArg(1).Cast(_model);
                processor.LoadArg(2);
                var fh = _info.KeyMembers[0];
                if (fh.MemberType == typeof(long))
                {
                    processor.Call(_handler.ConvertToInt64);
                }
                else if (fh.MemberType == typeof(int))
                {
                    processor.Call(_handler.ConvertToInt32);
                }
                else if (fh.MemberType == typeof(Guid))
                {
                    processor.Unbox(_handler.Import(h.MemberType));
                }
                else
                {
                    processor.Cast(_handler.Import(h.MemberType));
                }
                processor.SetMember(h, _handler);
            }

            processor.Return();
            processor.Append();
            _result.Methods.Add(method);
        }