473,394 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Getting operators using Reflection

Plater
7,872 Expert 4TB
I am having some trouble finding a way to retreive the operators from a custom class.

In a simplified example I have the following:
Expand|Select|Wrap|Line Numbers
  1. public class Namespace1.Address
  2. {
  3.     public string City;
  4.     public string State;
  5.  
  6.     public static implicit operator Address(Namespace2.Address value)
  7.     { /* Logic to transform */ }
  8. }
  9.  
  10. public class Namespace2.Address
  11. {
  12.     public string City;
  13.     public string State;
  14.  
  15.     public static implicit operator Address(Namespace1.Address value)
  16.     { /* Logic to transform */ }
  17. }
  18.  
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.GetMembers(BindingFlags.Public|BindingFlag s.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:

Expand|Select|Wrap|Line Numbers
  1. public class Namespace1.Location
  2. {
  3.     public string Name;
  4.     public Address Place;    
  5. }
  6.  
  7. public class Namespace2.Location
  8. {
  9.     public string Name;
  10.     public Address Place;
  11. }
  12.  
(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.
Dec 3 '10 #1
3 8435
Plater
7,872 Expert 4TB
I think I may have found what I am looking for:

Expand|Select|Wrap|Line Numbers
  1. //EndType is the type I want to PRODUCE
  2. //StartType is the type I am pulling data FROM
  3. MethodInfo mi = EndType.GetMethod(
  4.     "op_Implicit", 
  5.     (BindingFlags.Public | BindingFlags.Static), 
  6.     null, 
  7.     new Type[] { StartType }, 
  8.     new ParameterModifier[0]
  9. );
  10. //Now if mi is NOT null, it means there is an operator that allows for converting StartType to EndType, if it is null, there isn't one
  11.  
Dec 3 '10 #2
Plater
7,872 Expert 4TB
So I think I have what I wanted, it's been working well.
Expand|Select|Wrap|Line Numbers
  1. public static class ShippingConversion
  2. {
  3.   private static BindingFlags basePropertyFlags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public;
  4.   public static object ConvertOP(object StartObject, Type EndType)
  5.   {
  6.         object retval = null;
  7.         Type[] SearchTypes = (StartObject != null) ? new Type[] { StartObject.GetType() } : new Type[0];
  8.         MethodInfo mi = EndType.GetMethod("op_Explicit", (BindingFlags.Public | BindingFlags.Static), null, SearchTypes, new ParameterModifier[0]);
  9.         if (mi == null) mi = EndType.GetMethod("op_Implicit", (BindingFlags.Public | BindingFlags.Static), null, SearchTypes, new ParameterModifier[0]);
  10.         if (mi != null)//there is a conversion operator!
  11.         { retval = EndType.InvokeMember(mi.Name, BindingFlags.InvokeMethod | (BindingFlags.Public | BindingFlags.Static), null, null, new object[] { StartObject }); }
  12.         return retval;
  13.   }
  14.     public static object Convert(object StartObject, Type EndType)
  15.     {
  16.         object retval = null;
  17.  
  18.         Type StartType = StartObject.GetType();
  19.         retval = EndType.InvokeMember("", BindingFlags.CreateInstance, null, null, new object[0]);
  20.         PropertyInfo[] pis = EndType.GetProperties(basePropertyFlags);
  21.         foreach (PropertyInfo oI in pis)//you should probably use getFields, in case there is no setter, but in my case this is -safe-(ish)
  22.         {
  23.             PropertyInfo thisFIs = StartType.GetProperty(oI.Name, basePropertyFlags);
  24.             if ((thisFIs != null))
  25.             {
  26.                 object cVal = StartType.InvokeMember(thisFIs.Name, basePropertyFlags | BindingFlags.GetProperty, null, StartObject, new object[0]);
  27.                 if (thisFIs.PropertyType == oI.PropertyType)
  28.                 { EndType.InvokeMember(thisFIs.Name, basePropertyFlags | BindingFlags.SetProperty, null, retval, new object[] { cVal }); }
  29.                 else
  30.                 {//check for operator for it?
  31.                     object o = ConvertOP(cVal, oI.PropertyType);
  32.                     if (o != null) EndType.InvokeMember(thisFIs.Name, basePropertyFlags | BindingFlags.SetProperty, null, retval, new object[] { o });
  33.                 }
  34.             }
  35.         }
  36.  
  37.         return retval;
  38.     }
  39. } //end of class
  40.  
Anything that is the -same- type and -same- name gets set. If they are custom classes, the code checks to see if there are implicit/explict operators for it. (Those are used for type-casting)

I'm pretty pleased that this is working.
Dec 3 '10 #3
GaryTexmo
1,501 Expert 1GB
Nifty... nice job :)
Dec 3 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Dawid Mostert | last post by:
Hi, I'm trying to get a list of properties from a hierarchy of classes, using reflection. Using Type.GetProperties(), I get the list back in "reverse hierarchy order" (derived class...
1
by: Suresh | last post by:
Hi, How to set the TextBox text property using Reflection. Or to that matter any property of a control. Iam having trouble writing a function of the style public static void...
0
by: samlee | last post by:
Hi All, I'm learning how to write C# using reflection, but don't know how to code using reflection this.Controls.Add(this.label1); Could anyone help, Thank in advance. ...
7
by: maf | last post by:
Using reflection, I'm trying to get the value for a constant in an enum. Getting the contant name works fine using: FieldInfo fieldInfos = TYPE.GetFields(); foreach(FieldInfo fi in fieldInfos...
1
by: Mudassar | last post by:
i want to get the property value using reflection. Scenerio: i have a status bar on MDI form. it has property named "Panels" and i want to get a specific panel from that panels collection using...
3
by: trevorelbourne | last post by:
Hi, I am having trouble accessing the elements of an array using reflection. This is the code I am having trouble with: FieldInfo Fields = Obj.GetType().GetFields(); foreach (FieldInfo fi in...
6
by: Ken Varn | last post by:
I have an ASP.NET application that is calling a custom class that is trying to parse all of the members of my Page object using Type.GetMembers(). The problem that I am having is that private...
3
by: Steve Amey | last post by:
Hi all I am using reflection to read the values of properties from a class. The class is returned from a Web Service so I have to access the class using FieldInfo (Using VS 2003 which converts...
6
by: =?Utf-8?B?SmFzb24gUmV5bm9sZHM=?= | last post by:
(using .net 2.0) Say you have a class structure like this: class Address .... end class class Person FirstName
4
by: =?Utf-8?B?QWJoaQ==?= | last post by:
I am using Reflection to invoke methods dynamically. I have got a special requirement where I need to pass a value to method by setting the custom method attribute. As I cannot change the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.