My class MyClass<t>
has the following method
private static bool TypeHasProperty(string NameOfPropertyToMatch)
{
var properties = typeof(t).GetProperties();
var requiredProperty = properties.First(
a => a.Name == NameOfPropertyToMatch);
if (requiredProperty == null)
{
return false
};
return true;
}
This is called at the start of a static method:
MyClass<t>.SendToJavaScript(t InstanceOfType, string NameOfPropertyWithinType)
.
to make sure that InstanceOfType
actually has a property of that name before continuing, because otherwise an exception wouldn't get thrown until waaaaay down the line
(this information will eventually get serialized and sent to a Javascript Application, which needs to know which property to access in order to do it's job).
I would like for a good, type-safe way to call that method that will not cause the validation to fail if I decide to change the name of my properties later on down the line.
For instance, I do NOT want to call it like this:
MyClass<Person>.SendToJavaScript(MyPerson, "Name")
Because if I decide to change Name
to LastName,
in the Person
class, I would have to make sure to come in and change that magic string.
What I WOULD want, is something like this:
MeClass<Person>.SendToJavaScript(
MyPerson,
TypeOf(Person).Properties.Name.ToString())
so that if I use Refactoring to change Name
to LastName
, the IDE will sweep through there and change my call as well.
Does anything like that exist? Or do I just need to be careful when I refactor? (I love me some F2
)