Пример #1
0
        protected FieldInfo GrabField(FieldInfo Field, TypeBuilder On)
        {
            if (Field == null)
            {
                return(null);
            }

            if (FieldsDone.ContainsKey(Field))
            {
                return(FieldsDone[Field]);
            }

            if (!Sources.Contains(Field.Module))
            {
                return(Field);
            }

            if (On == null)
            {
                On = GrabType(Field.DeclaringType) as TypeBuilder;
            }

            FieldBuilder CopiedField = On.DefineField(Field.Name, GrabType(Field.FieldType), Field.Attributes);

            FieldsDone.Add(Field, CopiedField);

            if (Field.IsLiteral && Field.DeclaringType.IsEnum)
            {
                CopiedField.SetConstant(Field.GetRawConstantValue());
            }

            FieldOffsetAttribute Attr = FindCustomAttribute <FieldOffsetAttribute>(Field);

            if (Attr != null)
            {
                CopiedField.SetOffset(Attr.Value);
            }

            // Fields like these mostly have a backing field that comes with them in the
            // <PrivateImplementationDetails> class of the assembly. This backing field
            // refers to a bit of data in the .sdata section, which is serialized in a
            // way not exposed by the .NET framework. The best thing we can do is to try
            // to replicate this format.
            if (Field.IsStatic && Field.IsInitOnly)
            {
                object Val     = Field.GetValue(null);
                Type   ValType = Val.GetType();

                if (ValType.IsArray)
                {
                    byte[]       Raw     = RawSerialize((Array)Val, ValType.GetElementType());
                    FieldBuilder Backing = ImplementationDetails.DefineInitializedData(Field.Name, Raw, Field.Attributes);

                    // This is used later on when we recognize the initilization pattern (see TryReplaceBackingField)
                    BackingFields.Add(CopiedField, Backing);
                }
            }

            return(CopiedField);
        }
        public void GetValue_NotInitialized_DefaultValue()
        {
            var backingFileds = new BackingFields(null);

            Assert.AreEqual(null, backingFileds.GetValue(() => StringProperty));
            Assert.AreEqual(0, backingFileds.GetValue(() => IntProperty));
            Assert.AreEqual(default(DateTime), backingFileds.GetValue(() => DateTimeProperty));
            Assert.AreEqual(null, backingFileds.GetValue(() => NullableDateTimeProperty));
            Assert.AreEqual(null, backingFileds.GetValue(() => StringListProperty));
            Assert.AreEqual(null, backingFileds.GetValue(() => ViewModelProperty));
        }
        public void GetValue_Initialized_SameValue()
        {
            var backingFileds = new BackingFields(null);

            Assert.AreEqual("a", backingFileds.GetValue(() => StringProperty, () => "a"));
            Assert.AreEqual(7, backingFileds.GetValue(() => IntProperty, () => 7));
            Assert.AreEqual(new DateTime(2222, 2, 2), backingFileds.GetValue(() => DateTimeProperty, () => new DateTime(2222, 2, 2)));
            Assert.AreEqual(new DateTime(2345, 6, 7), backingFileds.GetValue(() => NullableDateTimeProperty, () => new DateTime(2345, 6, 7)));
            Assert.AreEqual("b", backingFileds.GetValue(() => StringListProperty, () => new List<string> { "b" })[0]);
            Assert.AreEqual("c", backingFileds.GetValue(() => ViewModelProperty, () => new ViewModel { Value = "c" }).Value);
        }
        public void SetValue_DefalutValue_SameValue()
        {
            var backingFileds = new BackingFields(null);

            backingFileds.SetValue(() => StringProperty, null);
            backingFileds.SetValue(() => IntProperty, 0);
            backingFileds.SetValue(() => DateTimeProperty, default(DateTime));
            backingFileds.SetValue(() => NullableDateTimeProperty, null);
            backingFileds.SetValue(() => StringListProperty, null);
            backingFileds.SetValue(() => ViewModelProperty, null);

            Assert.AreEqual(null, backingFileds.GetValue(() => StringProperty));
            Assert.AreEqual(0, backingFileds.GetValue(() => IntProperty));
            Assert.AreEqual(default(DateTime), backingFileds.GetValue(() => DateTimeProperty));
            Assert.AreEqual(null, backingFileds.GetValue(() => NullableDateTimeProperty));
            Assert.AreEqual(null, backingFileds.GetValue(() => StringListProperty));
            Assert.AreEqual(null, backingFileds.GetValue(() => ViewModelProperty));
        }
            public CommandBarActionDef(VsShortcutFinder vsShortcutFinder, DTE dte, string actionId,
                                       CommandID commandId, CommandBarControl control,
                                       CommandBarPopup[] parentPopups)
            {
                ActionId = actionId;

                // Lazily initialise. Talking to the command bar objects is SLOOOOOOOWWWWWW.
                backingFields = Lazy.Of(() =>
                {
                    Assertion.AssertNotNull(control, "control != null");

                    var sb = new StringBuilder();
                    foreach (var popup in parentPopups)
                    {
                        sb.AppendFormat("{0} \u2192 ", popup.Caption);
                    }

                    var fields = new BackingFields
                    {
                        Text = MnemonicStore.RemoveMnemonicMark(control.Caption),
                        Path = MnemonicStore.RemoveMnemonicMark(sb.ToString())
                    };

                    var command    = VsCommandHelpers.TryGetVsCommandAutomationObject(commandId, dte);
                    var vsShortcut = vsShortcutFinder.GetVsShortcut(command);
                    if (vsShortcut != null)
                    {
                        var details = new ShortcutDetails[vsShortcut.KeyboardShortcuts.Length];
                        for (int i = 0; i < vsShortcut.KeyboardShortcuts.Length; i++)
                        {
                            var keyboardShortcut = vsShortcut.KeyboardShortcuts[i];
                            details[i]           = new ShortcutDetails(KeyConverter.Convert(keyboardShortcut.Key),
                                                                       keyboardShortcut.Modifiers);
                        }
                        fields.VsShortcut = new ShortcutSequence(details);
                    }

                    return(fields);
                }, true);
            }
        public void SetValue_NotDefalutValue_SameValue()
        {
            var backingFileds = new BackingFields(null);

            Assert.IsTrue(backingFileds.SetValue(() => StringProperty, "a"));
            Assert.IsTrue(backingFileds.SetValue(() => IntProperty, 7));
            Assert.IsTrue(backingFileds.SetValue(() => DateTimeProperty, new DateTime(2222, 2, 2)));
            Assert.IsTrue(backingFileds.SetValue(() => NullableDateTimeProperty, new DateTime(2345, 6, 7)));
            Assert.IsTrue(backingFileds.SetValue(() => StringListProperty, new List<string> { "b" }));
            Assert.IsTrue(backingFileds.SetValue(() => ViewModelProperty, new ViewModel { Value = "c" }));

            Assert.AreEqual("a", backingFileds.GetValue(() => StringProperty));
            Assert.AreEqual(7, backingFileds.GetValue(() => IntProperty));
            Assert.AreEqual(new DateTime(2222, 2, 2), backingFileds.GetValue(() => DateTimeProperty));
            Assert.AreEqual(new DateTime(2345, 6, 7), backingFileds.GetValue(() => NullableDateTimeProperty));
            Assert.AreEqual("b", backingFileds.GetValue(() => StringListProperty)[0]);
            Assert.AreEqual("c", backingFileds.GetValue(() => ViewModelProperty).Value);

            Assert.IsFalse(backingFileds.SetValue(() => StringProperty, "a"));
            Assert.IsFalse(backingFileds.SetValue(() => IntProperty, 7));
            Assert.IsFalse(backingFileds.SetValue(() => DateTimeProperty, new DateTime(2222, 2, 2)));
            Assert.IsFalse(backingFileds.SetValue(() => NullableDateTimeProperty, new DateTime(2345, 6, 7)));
            Assert.IsFalse(backingFileds.SetValue(() => StringListProperty, backingFileds.GetValue(() => StringListProperty)));
            Assert.IsFalse(backingFileds.SetValue(() => ViewModelProperty, backingFileds.GetValue(() => ViewModelProperty)));
        }
        public void SetValueAndObserve_SetNull_NoException()
        {
            var backingFileds = new BackingFields(null);

            backingFileds.SetValueAndObserve(() => ViewModelProperty, new ViewModel());
            backingFileds.SetValueAndObserve(() => ViewModelProperty, null);
        }
        public void GetCommand()
        {
            var counter = 0;
            var backingFileds = new BackingFields(null);

            var command = backingFileds.GetCommand(() => CommandProperty, () => { counter++; });
            command.Execute(null);
            Assert.AreEqual(1, counter);

            command.Execute(null);
            Assert.AreEqual(2, counter);
        }
        public void GetValue_Initialized_OnlyOnce()
        {
            var counter = 0;
            Func<ViewModel> init = () => { counter++; return new ViewModel(); };
            var backingFileds = new BackingFields(null);
            
            backingFileds.GetValue(() => ViewModelProperty, init);
            Assert.AreEqual(1, counter);

            backingFileds.GetValue(() => ViewModelProperty, init);
            Assert.AreEqual(1, counter);
        }
        public void GetInitializedAndSet_EventHandler_AttachedAndDetached()
        {
            var viewModel1 = new ViewModel { Value = "a" };
            var viewModel2 = new ViewModel { Value = "r" };
            var counter1 = 0;
            var counter2 = 0;

            var backingFileds = new BackingFields(
                s =>
                {
                    if (s == "ViewModelProperty")
                    {
                        counter1++;
                    }
                });

            PropertyChangedEventHandler handler = (source, args) =>
                {
                    if (args.PropertyName == "Value")
                    {
                        counter2++;
                    }
                };

            backingFileds.GetValueAndObserve(() => ViewModelProperty, () => viewModel1, handler);
            Assert.AreEqual(0, counter1);
            Assert.AreEqual(0, counter2);

            viewModel1.Value = "b";
            Assert.AreEqual(0, counter1);
            Assert.AreEqual(1, counter2);

            backingFileds.SetValueAndObserve(() => ViewModelProperty, viewModel2, handler);
            Assert.AreEqual(1, counter1);
            Assert.AreEqual(1, counter2);

            viewModel1.Value = "c";
            Assert.AreEqual(1, counter1);
            Assert.AreEqual(1, counter2);

            viewModel2.Value = "s";
            Assert.AreEqual(1, counter1);
            Assert.AreEqual(2, counter2);
        }
        public void GetInitializedAndSet_Observer_AttachedAndDetached()
        {
            var viewModel1 = new ViewModel { Value = "a" };
            var viewModel2 = new ViewModel { Value = "r" };
            var counter = 0;

            var backingFileds = new BackingFields(
                s =>
                {
                    if (s == "ViewModelProperty")
                    {
                        counter++;
                    }
                });

            backingFileds.GetValueAndObserve(() => ViewModelProperty, () => viewModel1);
            Assert.AreEqual(0, counter);

            viewModel1.Value = "b";
            Assert.AreEqual(1, counter);

            backingFileds.SetValueAndObserve(() => ViewModelProperty, viewModel2);
            Assert.AreEqual(2, counter);

            viewModel1.Value = "c";
            Assert.AreEqual(2, counter);

            viewModel2.Value = "s";
            Assert.AreEqual(3, counter);
        }