none
CSharp Equivalent of java.lang.Class<?> using Visual Studio 2008 SP1 RRS feed

  • Question

  • Dear All,

    I am looking for CSharp Equivalent of java.lang.Class<?> using Visual Studio 2008 SP1.

    I hope someone will advise me to get it done.

    Thanks and best regards

    Monday, February 4, 2013 8:11 AM

Answers

  • Below is working code for your problem. Pay special attention to object.GetType(), Type.GetType("StringTypeOfClassOrEnum") and typeof(someClass). 

            public static void ClassList()
            {
                Dictionary<int, Type> classes = new Dictionary<int, Type>();
                //Dictionary<int, Customer> customers = new Dictionary<int, Customer>();
    
                string pkg = "Test";
    
    
                Customer cust1 = new Customer(1, "Cust 1");  // Do I need this one?
    
                classes.Add(1,cust1.GetType());
                // customers.Add(cust1.ID, cust1);
    
                foreach (KeyValuePair<int, Type> custKeyVal in classes)
                {
                    Console.WriteLine(
                        "Customer ID: {0}, Name: {1}",
                        custKeyVal.Key,
                        custKeyVal.Value.Name);
                }
            }
    


    Thanks, Durgesh Chaudhary. http://home.techphernalia.com

    • Proposed as answer by Durgesh Chaudhary Wednesday, February 13, 2013 2:26 PM
    • Marked as answer by Mr Ko Ko Thursday, February 14, 2013 2:01 AM
    • Unmarked as answer by Mr Ko Ko Thursday, February 14, 2013 2:01 AM
    • Unproposed as answer by Mr Ko Ko Thursday, February 14, 2013 2:01 AM
    • Marked as answer by Mr Ko Ko Thursday, February 14, 2013 2:02 AM
    Wednesday, February 13, 2013 2:26 PM

All replies

    • Proposed as answer by octopus-jelly Monday, February 4, 2013 8:56 AM
    Monday, February 4, 2013 8:25 AM
    Moderator
  • Hi Mike,

    True but I couuldn't manage to find the working sample of it to understand more and pls advise me.

    Best regards

    Monday, February 4, 2013 9:47 AM
  • Working sample of what, what are you trying to do?
    Monday, February 4, 2013 9:52 AM
    Moderator
  • Hi,

    I mean working sample using System.Type since here is usage for Java

    // in Java
    
    private static HashMap<Byte, Class<?> > classes = null;
    
    public static Class<?> getTypeClass(byte type) 
     {
            Byte bt = new Byte(type);
            if (classes == null) 
            {
                ClassList();
            }
            return classes.get(bt);
    }

    I am looking for same or similar as above and pls advise me to solve the issue.

    Thanks and best regards

    Tuesday, February 5, 2013 2:09 AM
  • I'm not sure what to make of that example, in particular I'm not sure what Class<?> is supposed to be. It looks like a generic class but the java.lang.Class is not generic.

    If I ignore this detail then the code can be easily converted to C#:

    private static Dictionary<byte, Type> classes;
    
    public static Type getTypeClass(byte type) {
        if (classes == null) {
            ClassList();
        }
        Type result;
        classes.TryGetValue(type, out result);
        return result;
    }

    Tuesday, February 5, 2013 5:24 AM
    Moderator
  • Hi,

    Here is my java code below

    // in Java
    private static HashMap<Integer, Class<?> > classes = null;
    
    private static synchronized void classLists() throws ClassNotFoundException
    {
        classes = new HashMap<Integer, Class<?> >(10);
        String pkg = "Test";
           
        classes.put(new Integer(M1.TYPE1), Class.forName(pkg + "M1")); //  Here is 
    }
    
    
    
    // in .NET
    private static Dictionary<int, Type> classes = null;
    
    [MethodImpl(MethodImplOptions.Synchronized)]
    private static void ClassList()
    {
       classes = new Dictionary<int, Type>(10);
       string pkg = "Test";
    
       classes.Add(M1.TYPE1, ?);   // How to convert here
    
    }
    
    		
    
    

    I am stuck on this point and I hope someone will advise me to get it done.

    Thanks and best regards


    Friday, February 8, 2013 2:36 AM
  • The equivalent for Class.forName is Type.GetType (http://msdn.microsoft.com/EN-US/library/w3f99sx1(v=VS.110,d=hv.2).aspx) or Assembly.GetType (http://msdn.microsoft.com/EN-US/library/y0cd10tb(v=VS.110,d=hv.2).aspx).

    Problem is that you'll have to figure out where to get the assembly or assembly name from. Type.GetType needs to be called with a assembly qualified type name, like:

    Type.GetType("System.Net.Sockets.Socket, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");

    Friday, February 8, 2013 6:21 AM
    Moderator
  • private static Dictionary<int, Type> classes = null;
    
    [MethodImpl(MethodImplOptions.Synchronized)]
    private static void ClassList()
    {
       classes = new Dictionary<int, Type>(10);
       string pkg = "Test";
    
       classes.Add(M1.TYPE1, Type.GetType("WhatEverYouString"));   // Use it like this
    
    }
    Let us know if it solves your issue.

    Thanks, Durgesh Chaudhary. http://home.techphernalia.com

    • Proposed as answer by Durgesh Chaudhary Friday, February 8, 2013 9:04 AM
    • Marked as answer by Mr Ko Ko Tuesday, February 12, 2013 12:50 PM
    • Unmarked as answer by Mr Ko Ko Wednesday, February 13, 2013 2:14 AM
    Friday, February 8, 2013 9:04 AM
  • Hi,

    Thank you for your advice and in Java the return is Class<?>.

    But I am concern that return similarity for CSharp so could it be possible to have a working code snippet if you don't mind.

    Here is mine and pls advise me

    // in CSharp
    namespace PortingMethodTest
    {
        class Program
        {
            ClassList();
    
            [MethodImpl(MethodImplOptions.Synchronized)]
            public static void ClassList()
            {
               Dictionary<int, Type> classes = new Dictionary<int,Type>();
               //Dictionary<int, Customer> customers = new Dictionary<int, Customer>();
     
              string pkg = "Test";
           
          
               // Customer cust1 = new Customer(1, "Cust 1");  // Do I need this one?
    
                classes.Add(1, Type.GetType("cust1"));
                // customers.Add(cust1.ID, cust1);
    
                foreach (KeyValuePair<int, Customer> custKeyVal in customers)
                {
                    Console.WriteLine(
                        "Customer ID: {0}, Name: {1}",
                        custKeyVal.Key,
                        custKeyVal.Value.Name);
                }
            }
    
        }
    
        
        public class Customer
        {
            public Customer(int id, string name)
            {
                ID = id;
                Name = name;
            }
    
            private int m_id;
    
            public int ID
            {
                get { return m_id; }
                set { m_id = value; }
            }
    
            private string m_name;
    
            public string Name
            {
                get { return m_name; }
                set { m_name = value; }
            }
        }
      
    }
    
    
    
    
    

    Best regards

    Tuesday, February 12, 2013 1:25 PM
  • "to have a working code snippet if you don't mind."

    Working code snippet that does what? We don't know what the Java code is supposed to do and you can't translate code from one language to another without understanding what the original code does.

    Regarding your code sample:

    "// Do I need this one?"

    I've no idea why would you need that. It doesn't look anything like the original Java code you have posted.

    Type.GetType("cust1");

    There's no way that code will work. For one thing you haven't provided the assembly name as I mentioned in my previous post. And anyway that "cust1" doesn't appear to be the name of a type, it's just the name of a variable in that method.

    Tuesday, February 12, 2013 2:10 PM
    Moderator
  • Hi Mike.

    Here is the method definition for Class.forName in Java

    // in Method Definition in Java
    public static Class<?> forName(String className)
                            throws ClassNotFoundException
    Returns the Class object associated with the class or interface with the given string name. Invoking this method is equivalent to: 
      Class.forName(className, true, currentLoader)
     
    where currentLoader denotes the defining class loader of the current class. 
    For example, the following code fragment returns the runtime Class descriptor for the class named java.lang.Thread: 
    
       Class t = Class.forName("java.lang.Thread")
     
    A call to forName("X") causes the class named X to be initialized. 
    
    
    Parameters:
    className - the fully qualified name of the desired class. 
    Returns:
    the Class object for the class with the specified name. 
    Throws: 
    LinkageError - if the linkage fails 
    ExceptionInInitializerError - if the initialization provoked by this method fails 
    ClassNotFoundException - if the class cannot be located

    The input is String and the return output is Class in Java so I assume string input and Type output in CSharp.

    Hi Durgesh,

    Pls help me whether my assumpttion is correct with any working code snippet.

    Thanks and best regards

    Wednesday, February 13, 2013 2:11 AM
  • Below is working code for your problem. Pay special attention to object.GetType(), Type.GetType("StringTypeOfClassOrEnum") and typeof(someClass). 

            public static void ClassList()
            {
                Dictionary<int, Type> classes = new Dictionary<int, Type>();
                //Dictionary<int, Customer> customers = new Dictionary<int, Customer>();
    
                string pkg = "Test";
    
    
                Customer cust1 = new Customer(1, "Cust 1");  // Do I need this one?
    
                classes.Add(1,cust1.GetType());
                // customers.Add(cust1.ID, cust1);
    
                foreach (KeyValuePair<int, Type> custKeyVal in classes)
                {
                    Console.WriteLine(
                        "Customer ID: {0}, Name: {1}",
                        custKeyVal.Key,
                        custKeyVal.Value.Name);
                }
            }
    


    Thanks, Durgesh Chaudhary. http://home.techphernalia.com

    • Proposed as answer by Durgesh Chaudhary Wednesday, February 13, 2013 2:26 PM
    • Marked as answer by Mr Ko Ko Thursday, February 14, 2013 2:01 AM
    • Unmarked as answer by Mr Ko Ko Thursday, February 14, 2013 2:01 AM
    • Unproposed as answer by Mr Ko Ko Thursday, February 14, 2013 2:01 AM
    • Marked as answer by Mr Ko Ko Thursday, February 14, 2013 2:02 AM
    Wednesday, February 13, 2013 2:26 PM
  • Hi,

    Thanks a lot forgetting object.GetType() since lots of java codes and pls help me few critical porting like

    1. CSharp Equivalent of java.lang.reflect.Method

    http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/b142a980-b8ff-4f69-b681-982ee2720e06

    2. CSharp Equivalent of java.security.AlgorithmParameters, javax.crypto.spec.SecretKeySpec and  javax.crypto.Cipher using Visual Studio 2008 SP1

    http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/540fbe58-a19b-4c02-a47c-1a2628254cc6

    3. CSharp Equivalent of java.socket.getOutputStream and java.io.OutputStream using Visual Studio 2008 SP1

    http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/31153729-00cc-48f8-8c52-e077a6b0a9b1

    Thanks and best regards

    <form action="/Forums/en-US/search" id="forumsSearch" method="post"></form>
     
    Thursday, February 14, 2013 2:24 AM