There is actually a (subtle) difference between the two. Imagine you have the following code in File1.cs:
// File1.cs
using System;
namespace Outer.Inner
{
class Foo
{
static void Bar()
{
double d = Math.PI;
}
}
}
Now imagine that someone adds another file (File2.cs) to the project that looks like this:
// File2.cs
namespace Outer
{
class Math
{
}
}
The compiler searches Outer
before looking at those using
directives outside the namespace, so it finds Outer.Math
instead of System.Math
. Unfortunately (or perhaps fortunately?), Outer.Math
has no PI
member, so File1 is now broken.
This changes if you put the using
inside your namespace declaration, as follows:
// File1b.cs
namespace Outer.Inner
{
using System;
class Foo
{
static void Bar()
{
double d = Math.PI;
}
}
}
Now the compiler searches System
before searching Outer
, finds System.Math
, and all is well.
Some would argue that Math
might be a bad name for a user-defined class, since there's already one in System
; the point here is just that there is a difference, and it affects the maintainability of your code.
It's also interesting to note what happens if Foo
is in namespace Outer
, rather than Outer.Inner
. In that case, adding Outer.Math
in File2 breaks File1 regardless of where the using
goes. This implies that the compiler searches the innermost enclosing namespace before it looks at any using
directive.
Is the type under your control? You could add a FooUpdating
/ FooUpdated
pair of events? Another option would be to write a custom property-model with TypeDescriptionProvider
, but I suspect that would be quite a lot of work. My first attempt would be a before/after pair...
Something like (updated to show 3.5 approach; see history for a 2.0 example):
class MyType : INotifyPropertyChanged, INotifyPropertyChanging
{
public event PropertyChangedEventHandler PropertyChanged;
public event PropertyChangingEventHandler PropertyChanging;
protected void UpdateField<T>(ref T field, T newValue, string propertyName)
{
if (!EqualityComparer<T>.Default.Equals(field, newValue))
{
OnPropertyChanging(propertyName);
field = newValue;
OnPropertyChanged(propertyName);
}
}
protected void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this,
new PropertyChangedEventArgs(propertyName));
}
protected void OnPropertyChanging(string propertyName)
{
PropertyChangingEventHandler handler = PropertyChanging;
if (handler != null) handler(this,
new PropertyChangingEventArgs(propertyName));
}
private string name;
public string Name
{
get { return name; }
set { UpdateField(ref name, value, "Name"); }
}
private DateTime dateOfBirth;
public DateTime DateOfBirth
{
get { return dateOfBirth; }
set { UpdateField(ref dateOfBirth, value, "DateOfBirth"); }
}
}
Then just handle the two events and enable / disable updates as appropriate.
Best Answer
You could create a derived subclass which overrides the properties and uses Invoke to set the properties in a "thread-safe" manner.
Invoke() runs the delegate that you pass on the same thread that created the control, so it's implicitly thread-safe. However, this could be a lot of work if you have a lot of controls you need to subclass.
It would probably be better worth your time to figure out why you are getting threading issues - if both controls are created on the same thread (as is normal for a Windows app) you shouldn't be getting these exceptions. Are you creating the PropertyGrid form on a different thread for some reason?