SetProperty!!!
-
public static class Extensions
{
public static void SetProperty(this object domainEntity, string propertyName, object value)
{
var t = domainEntity.GetType();
var p = t.GetProperty(propertyName, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
p.SetValue(domainEntity, value);
}
public static void SetProperty<T, TProp>(this T domainEntity, Expression<Func<T, TProp>> getP, TProp value)
{
var e = (MemberExpression)getP.Body;
SetProperty(domainEntity, e.Member.Name, value);
}
}This piece of code has a fantastic history that is not obvious... Why is that you might ask? Well.. the single architect of our application decided that we were going to use Domain Driven Design (DDD). He also decided that all domain object property will have private setters. This being, as you can imagine, inconvenient, we got large use of this method (which he also wrote) to set private properties whenever needed! Brilliant! Let's use private property for safety! And let's use this method to ignore private! Genius! :suss: :-\ :-D
My programming get away... The Blog... DirectX for WinRT/C# since 2013! Taking over the world since 1371!
-
public static class Extensions
{
public static void SetProperty(this object domainEntity, string propertyName, object value)
{
var t = domainEntity.GetType();
var p = t.GetProperty(propertyName, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
p.SetValue(domainEntity, value);
}
public static void SetProperty<T, TProp>(this T domainEntity, Expression<Func<T, TProp>> getP, TProp value)
{
var e = (MemberExpression)getP.Body;
SetProperty(domainEntity, e.Member.Name, value);
}
}This piece of code has a fantastic history that is not obvious... Why is that you might ask? Well.. the single architect of our application decided that we were going to use Domain Driven Design (DDD). He also decided that all domain object property will have private setters. This being, as you can imagine, inconvenient, we got large use of this method (which he also wrote) to set private properties whenever needed! Brilliant! Let's use private property for safety! And let's use this method to ignore private! Genius! :suss: :-\ :-D
My programming get away... The Blog... DirectX for WinRT/C# since 2013! Taking over the world since 1371!
At least,
static
properties are still safe. -
public static class Extensions
{
public static void SetProperty(this object domainEntity, string propertyName, object value)
{
var t = domainEntity.GetType();
var p = t.GetProperty(propertyName, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
p.SetValue(domainEntity, value);
}
public static void SetProperty<T, TProp>(this T domainEntity, Expression<Func<T, TProp>> getP, TProp value)
{
var e = (MemberExpression)getP.Body;
SetProperty(domainEntity, e.Member.Name, value);
}
}This piece of code has a fantastic history that is not obvious... Why is that you might ask? Well.. the single architect of our application decided that we were going to use Domain Driven Design (DDD). He also decided that all domain object property will have private setters. This being, as you can imagine, inconvenient, we got large use of this method (which he also wrote) to set private properties whenever needed! Brilliant! Let's use private property for safety! And let's use this method to ignore private! Genius! :suss: :-\ :-D
My programming get away... The Blog... DirectX for WinRT/C# since 2013! Taking over the world since 1371!
Not only that - because you have to refer to the property by name you also break compile time type-checking.
-
Not only that - because you have to refer to the property by name you also break compile time type-checking.
I guess that's where the second overload comes in:
codeReview.SetProperty(review => review.Status, Status.WhatTheElephant);
It's not fast, pretty, or even remotely sensible, but at least it's strongly typed and the property name is checked by the compiler.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
I guess that's where the second overload comes in:
codeReview.SetProperty(review => review.Status, Status.WhatTheElephant);
It's not fast, pretty, or even remotely sensible, but at least it's strongly typed and the property name is checked by the compiler.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Yeah - Ironically the non type safe overload shouldn't be public :-)
-
public static class Extensions
{
public static void SetProperty(this object domainEntity, string propertyName, object value)
{
var t = domainEntity.GetType();
var p = t.GetProperty(propertyName, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
p.SetValue(domainEntity, value);
}
public static void SetProperty<T, TProp>(this T domainEntity, Expression<Func<T, TProp>> getP, TProp value)
{
var e = (MemberExpression)getP.Body;
SetProperty(domainEntity, e.Member.Name, value);
}
}This piece of code has a fantastic history that is not obvious... Why is that you might ask? Well.. the single architect of our application decided that we were going to use Domain Driven Design (DDD). He also decided that all domain object property will have private setters. This being, as you can imagine, inconvenient, we got large use of this method (which he also wrote) to set private properties whenever needed! Brilliant! Let's use private property for safety! And let's use this method to ignore private! Genius! :suss: :-\ :-D
My programming get away... The Blog... DirectX for WinRT/C# since 2013! Taking over the world since 1371!
Dagnabit, I keep hitting upvote, but it only registered once. :sigh: That's right up there with the "Singleton" articles that get posted here that require the target class to have a private constructor and then use Reflection to get it. :doh:
You'll never get very far if all you do is follow instructions.
-
public static class Extensions
{
public static void SetProperty(this object domainEntity, string propertyName, object value)
{
var t = domainEntity.GetType();
var p = t.GetProperty(propertyName, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
p.SetValue(domainEntity, value);
}
public static void SetProperty<T, TProp>(this T domainEntity, Expression<Func<T, TProp>> getP, TProp value)
{
var e = (MemberExpression)getP.Body;
SetProperty(domainEntity, e.Member.Name, value);
}
}This piece of code has a fantastic history that is not obvious... Why is that you might ask? Well.. the single architect of our application decided that we were going to use Domain Driven Design (DDD). He also decided that all domain object property will have private setters. This being, as you can imagine, inconvenient, we got large use of this method (which he also wrote) to set private properties whenever needed! Brilliant! Let's use private property for safety! And let's use this method to ignore private! Genius! :suss: :-\ :-D
My programming get away... The Blog... DirectX for WinRT/C# since 2013! Taking over the world since 1371!
:doh: :wtf:
What do you get when you cross a joke with a rhetorical question?
-
I guess that's where the second overload comes in:
codeReview.SetProperty(review => review.Status, Status.WhatTheElephant);
It's not fast, pretty, or even remotely sensible, but at least it's strongly typed and the property name is checked by the compiler.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
I created this second overload 2 days ago!! :laugh:
My programming get away... The Blog... DirectX for WinRT/C# since 2013! Taking over the world since 1371!
-
Dagnabit, I keep hitting upvote, but it only registered once. :sigh: That's right up there with the "Singleton" articles that get posted here that require the target class to have a private constructor and then use Reflection to get it. :doh:
You'll never get very far if all you do is follow instructions.
That's just as bad, indeed!!! :laugh:
My programming get away... The Blog... DirectX for WinRT/C# since 2013! Taking over the world since 1371!
-
I guess that's where the second overload comes in:
codeReview.SetProperty(review => review.Status, Status.WhatTheElephant);
It's not fast, pretty, or even remotely sensible, but at least it's strongly typed and the property name is checked by the compiler.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
well, you can't use this line of code outside of review.GetType() class... Status is private ;)
-
public static class Extensions
{
public static void SetProperty(this object domainEntity, string propertyName, object value)
{
var t = domainEntity.GetType();
var p = t.GetProperty(propertyName, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
p.SetValue(domainEntity, value);
}
public static void SetProperty<T, TProp>(this T domainEntity, Expression<Func<T, TProp>> getP, TProp value)
{
var e = (MemberExpression)getP.Body;
SetProperty(domainEntity, e.Member.Name, value);
}
}This piece of code has a fantastic history that is not obvious... Why is that you might ask? Well.. the single architect of our application decided that we were going to use Domain Driven Design (DDD). He also decided that all domain object property will have private setters. This being, as you can imagine, inconvenient, we got large use of this method (which he also wrote) to set private properties whenever needed! Brilliant! Let's use private property for safety! And let's use this method to ignore private! Genius! :suss: :-\ :-D
My programming get away... The Blog... DirectX for WinRT/C# since 2013! Taking over the world since 1371!
It remind me when I wrote
static bool Is(this T obj);
with reflection... Then a friend of mind said : WTF have you written that ? instead of "obj is T". The worse of it is that I had no idea why I did something so stupid... wanted to blame somebody else, but the source control history pointed at me. :D
-
It remind me when I wrote
static bool Is(this T obj);
with reflection... Then a friend of mind said : WTF have you written that ? instead of "obj is T". The worse of it is that I had no idea why I did something so stupid... wanted to blame somebody else, but the source control history pointed at me. :D
Did you mean
static bool Is<T>(this **object** obj);
? BecauseT obj
is... always...T
as far as I can tell! :rolleyes: :laugh: Good one anyway! :-DMy programming get away... The Blog... DirectX for WinRT/C# since 2013! Taking over the world since 1371!
-
Did you mean
static bool Is<T>(this **object** obj);
? BecauseT obj
is... always...T
as far as I can tell! :rolleyes: :laugh: Good one anyway! :-DMy programming get away... The Blog... DirectX for WinRT/C# since 2013! Taking over the world since 1371!
yes it was this object good point ;)
-
public static class Extensions
{
public static void SetProperty(this object domainEntity, string propertyName, object value)
{
var t = domainEntity.GetType();
var p = t.GetProperty(propertyName, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
p.SetValue(domainEntity, value);
}
public static void SetProperty<T, TProp>(this T domainEntity, Expression<Func<T, TProp>> getP, TProp value)
{
var e = (MemberExpression)getP.Body;
SetProperty(domainEntity, e.Member.Name, value);
}
}This piece of code has a fantastic history that is not obvious... Why is that you might ask? Well.. the single architect of our application decided that we were going to use Domain Driven Design (DDD). He also decided that all domain object property will have private setters. This being, as you can imagine, inconvenient, we got large use of this method (which he also wrote) to set private properties whenever needed! Brilliant! Let's use private property for safety! And let's use this method to ignore private! Genius! :suss: :-\ :-D
My programming get away... The Blog... DirectX for WinRT/C# since 2013! Taking over the world since 1371!