[英]Setting a class property at run time in c# crashes Visual Studio
In working on a larger wp7 project, I kept coming across this issue: 在从事更大的wp7项目时,我不断遇到这个问题:
Any time I set a property in my code, it crashes Visual Studio 2010. So I decided to do a proper test to try to isolate the problem. 每当我在代码中设置属性时,它都会使Visual Studio 2010崩溃。因此,我决定进行适当的测试以尝试找出问题所在。 The following code is a short wp7 app example that has the same issue in the same way. 以下代码是一个简短的wp7应用示例,该示例以相同的方式具有相同的问题。 Whenever the code tries to set the name property, it crashes Visual Studio. 每当代码尝试设置name属性时,Visual Studio就会崩溃。
Notes: I have no add-ins installed, and when I try to run this in safe mode, I get the error "The Project Type is not supported by this installation" 注意:我没有安装任何加载项,当我尝试在安全模式下运行此加载项时,出现错误“此安装不支持项目类型”
Any help would be appreciated 任何帮助,将不胜感激
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();
}
}
}
}
Call Stack: 调用堆栈:
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'
You're calling the setter recursively: 您递归地调用设置器:
set
{
if (value != name)
{
name = value; // <-- Argh! Recursion!
hi(name);
}
}
If you want to implement the getter/setter that way, you'll need to use a private member variable: 如果要以这种方式实现getter / setter,则需要使用一个私有成员变量:
private string _name;
public string name
{
get { return _name; }
set
{
if (value != _name)
{
_name = value;
hi(_name);
}
}
}
You have an infinite recursive loop 您有一个无限的递归循环
This bit 这一点
if (value != name)
calls the getter of the property if (value != name)
调用属性的getter
eg this bit 例如这个位
get { return name; }
get { return name; }
which in turn calls itself until you blow the stack. get { return name; }
依次调用自己,直到炸毁堆栈。
You need to use a backing field to store value in a backing field; 您需要使用一个后备字段将值存储在一个后备字段中。
private string _name
public string name
{
get { return _name; }
set
{
// won't crash any more
if (value != _name)
{
_name = value;
hi(name);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.