none
Reflection: how can I get the FieldType for a string? RRS feed

Answers

  • Hi Arne,

    According to your description, your class type is string.

    As far as I know, the type of string is System.String.

    Let’s have a look at sample code below.

    static void Main(string[] args)
            {
                string s = "hello,Arne!";
                Console.WriteLine(s.GetType()); //output:     System.String
                Console.WriteLine(s.GetTypeCode());//output : String
            }

    >>I am trying to supply data to a web service and every data item needs a FieldType.

    As you said, I need you to confirm something.

    Is the FieldType needed in the web service the FieldType of the reflection?

    Could you please share the code related to the string of the web service project?

    Regards,

    Damon


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by Arne MN Friday, August 16, 2013 11:35 AM
    Friday, August 16, 2013 9:58 AM

All replies

  • What does the string contain, the field name?

    Wednesday, August 14, 2013 5:39 AM
    Moderator
  • Hi Arne,

    Welcome to the MSDN forum.

    According to your description, you want to get the FieldType for a string.

    In general scenario,we can do it by the following steps:

    1.        To get the FieldType property, first get the class Type.
    2.        From the Type, get the FieldInfo.
    3.        From the FieldInfo, get the FieldType value.

    I give you a sample here.

    using System;
    using System.Reflection;
    // Make a field. 
    public class Myfield
    {
        private string field = "private field";
    }
    public class Myfieldinfo
    {
        public static int Main()
        {
            Console.WriteLine ("\nReflection.FieldInfo");
            Myfield Myfield = new Myfield();
            // Get the type and FieldInfo.
            Type MyType = typeof(Myfield);
            FieldInfo Myfieldinfo = MyType.GetField("field", 
                BindingFlags.Instance|BindingFlags.NonPublic);
            // Get and display the FieldType.
            Console.Write ("\n{0}.", MyType.FullName);
            Console.Write ("{0} - ", Myfieldinfo.Name);
            Console.Write ("{0};", Myfieldinfo.GetValue(Myfield));
            Console.Write ("\nFieldType = {0}", Myfieldinfo.FieldType);
        return 0;
        }
    }
    If I misunderstand you ,just feel free to let me know.

    Have a nice day!

    Damon


    Damon Bu - MSFT
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, August 14, 2013 10:05 AM
  • Damon,

    My Class type is string. I don't have a field in a class. I am trying to supply data to a web service and every data item needs a FieldType.


    Certified Geek

    Wednesday, August 14, 2013 2:12 PM
  • Hi Arne,

    According to your description, your class type is string.

    As far as I know, the type of string is System.String.

    Let’s have a look at sample code below.

    static void Main(string[] args)
            {
                string s = "hello,Arne!";
                Console.WriteLine(s.GetType()); //output:     System.String
                Console.WriteLine(s.GetTypeCode());//output : String
            }

    >>I am trying to supply data to a web service and every data item needs a FieldType.

    As you said, I need you to confirm something.

    Is the FieldType needed in the web service the FieldType of the reflection?

    Could you please share the code related to the string of the web service project?

    Regards,

    Damon


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by Arne MN Friday, August 16, 2013 11:35 AM
    Friday, August 16, 2013 9:58 AM