I am having some trouble finding a way to retreive the operators from a custom class.
In a simplified example I have the following:
[code=c#]
public class Namespace1.Addr ess
{
public string City;
public string State;
public static implicit operator Address(Namespa ce2.Address value)
{ /* Logic to transform */ }
}
public class Namespace2.Addr ess
{
public string City;
public string State;
public static implicit operator Address(Namespa ce1.Address value)
{ /* Logic to transform */ }
}
[/code]
Now I want to use reflection to get the custom operators that exist there (Actually what I want is a bit different, but I *think* this will lead me to THAT goal), and then invoke them?
[code#]
Type myT=Namespace1. Address.GetType ();
MemberInfo[] mis=myT.GetMemb ers(BindingFlag s.Public|Bindin gFlags.Static);
[/code]
Now after starting this question I figured out that using the Static bindingflag gave me the op_implicit that I had defined.
I think I'm just not thinking this through well enough.
I have a number of classes just like that Address class, and I want to write a conversion function that checks for the existance of an operator (implicit or explicit) between two classes, and then invokes it.
A universal converter if you will.
I have a reflection-based function that uses getFields() that works on the Address classes without the operators being needed. But chokes on classes such as this:
[code=c#]
public class Namespace1.Loca tion
{
public string Name;
public Address Place;
}
public class Namespace2.Loca tion
{
public string Name;
public Address Place;
}
[/code]
(Since the two address classes are not the same, using invokeMember fails).
I'm not sure my question is clear, but if anyone has ideas I can attempt to make it more clear.
In a simplified example I have the following:
[code=c#]
public class Namespace1.Addr ess
{
public string City;
public string State;
public static implicit operator Address(Namespa ce2.Address value)
{ /* Logic to transform */ }
}
public class Namespace2.Addr ess
{
public string City;
public string State;
public static implicit operator Address(Namespa ce1.Address value)
{ /* Logic to transform */ }
}
[/code]
Now I want to use reflection to get the custom operators that exist there (Actually what I want is a bit different, but I *think* this will lead me to THAT goal), and then invoke them?
[code#]
Type myT=Namespace1. Address.GetType ();
MemberInfo[] mis=myT.GetMemb ers(BindingFlag s.Public|Bindin gFlags.Static);
[/code]
Now after starting this question I figured out that using the Static bindingflag gave me the op_implicit that I had defined.
I think I'm just not thinking this through well enough.
I have a number of classes just like that Address class, and I want to write a conversion function that checks for the existance of an operator (implicit or explicit) between two classes, and then invokes it.
A universal converter if you will.
I have a reflection-based function that uses getFields() that works on the Address classes without the operators being needed. But chokes on classes such as this:
[code=c#]
public class Namespace1.Loca tion
{
public string Name;
public Address Place;
}
public class Namespace2.Loca tion
{
public string Name;
public Address Place;
}
[/code]
(Since the two address classes are not the same, using invokeMember fails).
I'm not sure my question is clear, but if anyone has ideas I can attempt to make it more clear.
Comment