在c#运行时设置类属性会导致Visual Studio崩溃

本文关键字:Visual Studio 崩溃 属性 运行时 设置 | 更新日期: 2023-09-27 18:18:21

在一个更大的wp7项目中,我一直遇到这个问题:

每当我在代码中设置属性时,它都会使Visual Studio 2010崩溃。所以我决定做一个适当的测试,试图把问题隔离出来。以下代码是一个简短的wp7应用程序示例,以相同的方式具有相同的问题。每当代码试图设置name属性时,它就会使Visual Studio崩溃。

注意:我没有安装插件,当我尝试在安全模式下运行这个时,我得到错误"这个安装不支持项目类型"

如有任何帮助,不胜感激

using System;
using System.Collections.Generic;
using System.Windows;
using Microsoft.Phone.Controls;
namespace propertytest
{
    public partial class MainPage : PhoneApplicationPage
    {
        public string name
        {
            get { return name; }
            set
            {
                //crash!
                if (value != name)
                {
                    name = value;
                    hi(name); 
                }
            }
        }
        void hi(string name)
        {
            MessageBox.Show("hi "+name);
        }
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            // Set the data context of the listbox control to the sample data
            DataContext = App.ViewModel;
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
            name = "your name";
        }
        // Load data for the ViewModel Items
        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (!App.ViewModel.IsDataLoaded)
            {
                App.ViewModel.LoadData();
            }
        }
    }
}

调用堆栈:

propertytest.dll!propertytest.MainPage.name.set(string value) Line 14   C#
propertytest.dll!propertytest.MainPage.MainPage() Line 34 + 0xb bytes   C#
     mscorlib.dll!System.Reflection.RuntimeConstructorInfo.InternalInvoke(System.Reflection.RuntimeConstructorInfo rtci, System.Reflection.BindingFlags invokeAttr,     System.Reflection.Binder binder, object parameters, System.Globalization.CultureInfo culture, bool isBinderDefault, System.Reflection.Assembly caller, bool verifyAccess, ref     System.Threading.StackCrawlMark stackMark)    
mscorlib.dll!System.Reflection.RuntimeConstructorInfo.InternalInvoke(object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, object[] parameters, System.Globalization.CultureInfo culture, ref System.Threading.StackCrawlMark stackMark) + 0x114 bytes    
mscorlib.dll!System.Activator.InternalCreateInstance(System.Type type, bool nonPublic, ref System.Threading.StackCrawlMark stackMark) + 0xf0 bytes  
mscorlib.dll!System.Activator.CreateInstance(System.Type type) + 0x2 bytes  
Microsoft.Phone.dll!System.Windows.Navigation.PageResourceContentLoader.BeginLoad_OnUIThread(System.AsyncCallback userCallback, System.Windows.Navigation.PageResourceContentLoader.PageResourceContentLoaderAsyncResult result) + 0xe6 bytes   
Microsoft.Phone.dll!System.Windows.Navigation.PageResourceContentLoader.BeginLoad.AnonymousMethod__0(object args) + 0x11 bytes  
mscorlib.dll!System.Reflection.RuntimeMethodInfo.InternalInvoke(System.Reflection.RuntimeMethodInfo rtmi, object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, object parameters, System.Globalization.CultureInfo culture, bool isBinderDefault, System.Reflection.Assembly caller, bool verifyAccess, ref System.Threading.StackCrawlMark stackMark)   
mscorlib.dll!System.Reflection.RuntimeMethodInfo.InternalInvoke(object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, object[] parameters, System.Globalization.CultureInfo culture, ref System.Threading.StackCrawlMark stackMark) + 0x168 bytes 
mscorlib.dll!System.Reflection.MethodBase.Invoke(object obj, object[] parameters) + 0xa bytes   
mscorlib.dll!System.Delegate.DynamicInvokeOne(object[] args) + 0x98 bytes   
mscorlib.dll!System.MulticastDelegate.DynamicInvokeImpl(object[] args) + 0x8 bytes  
mscorlib.dll!System.Delegate.DynamicInvoke(object[] args) + 0x2 bytes   
System.Windows.dll!System.Windows.Threading.DispatcherOperation.Invoke() + 0xc bytes    
System.Windows.dll!System.Windows.Threading.Dispatcher.Dispatch(System.Windows.Threading.DispatcherPriority priority) + 0x83 bytes  
System.Windows.dll!System.Windows.Threading.Dispatcher.OnInvoke(object context) + 0x8 bytes 
System.Windows.dll!System.Windows.Hosting.CallbackCookie.Invoke(object[] args) + 0x19 bytes 
System.Windows.dll!System.Windows.Hosting.DelegateWrapper.InternalInvoke(object[] args) + 0x2 bytes 
System.Windows.RuntimeHost.dll!System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(System.IntPtr pHandle, int nParamCount, System.Windows.Hosting.NativeMethods.ScriptParam[] pParams, ref System.Windows.Hosting.NativeMethods.ScriptParam pResult) + 0x5e bytes 
[External Code] 

**Debug Output:**
'UI Task' (Managed): Loaded 'mscorlib.dll'
'UI Task' (Managed): Loaded 'System.Windows.RuntimeHost.dll'
'UI Task' (Managed): Loaded 'System.dll'
'UI Task' (Managed): Loaded 'System.Windows.dll'
'UI Task' (Managed): Loaded 'System.Net.dll'
'UI Task' (Managed): Loaded 'System.Core.dll'
'UI Task' (Managed): Loaded 'System.Xml.dll'
'UI Task' (Managed): Loaded ''Applications'Install'1782A5A4-3D00-47D3-A109-B889805E61F9'Install'propertytest.dll', Symbols loaded.
'UI Task' (Managed): Loaded 'Microsoft.Phone.dll'
'UI Task' (Managed): Loaded 'Microsoft.Phone.Interop.dll'
'UI Task' (Managed): Loaded ''Applications'Install'1782A5A4-3D00-47D3-A109-B889805E61F9'Install'Microsoft.Phone.Controls.dll'

在c#运行时设置类属性会导致Visual Studio崩溃

你在递归地调用setter:

        set
        {
            if (value != name)
            {
                name = value; // <-- Argh! Recursion!
                hi(name); 
            }
        }

如果你想用这种方式实现getter/setter,你需要使用私有成员变量:

       private string _name;
       public string name
       {
            get { return _name; }
            set
            {
                if (value != _name)
                {
                    _name = value;
                    hi(_name); 
                }
            }
        }

你有一个无限递归循环

这一点

if (value != name)调用属性

的getter

。这个

get { return name; },它依次调用自己,直到你吹掉堆栈。

你需要使用一个后备字段来存储后备字段中的值;

private string _name
 public string name
        {
            get { return _name; }
            set
            {
                // won't crash any more
                if (value != _name)
                {
                    _name = value;
                    hi(name); 
                }
            }
        }