0

I'd like to be able to get the values and count from any model I create.

For example let's say I have a model that looks like this.

public class test
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
}

I want to be able to write code that will look at the model and then get ID, Name, Address and put them into an array. And I don't want the values. But the values from the model. Not the data. As well as getting the count of the values. 3.

Prescient
  • 1,051
  • 3
  • 17
  • 42
  • "And I don't want the values. But the values from the model." Can you explain this more clearly? I don't understand the difference between the two. – D Stanley Mar 14 '16 at 14:37
  • I want to get what makes up the model. Using the model above I would want to look inside and get the values are ID, Name and Address. Then put that into an list or array that I could then use elsewhere. – Prescient Mar 14 '16 at 14:40
  • @Prescient I think I understand what you are looking for now. You can use reflection to extract the names into a list. Take a look at my edited answer. – Kevin Burdett Mar 14 '16 at 14:43
  • If your requirement happens to be to create a `json` object, look at the class `JavascriptSerializer` within `System.Web.Script.Serialization` It will do the grunt work for you – Eric Phillips Mar 14 '16 at 14:46

3 Answers3

2

EDIT: per your clarifications in the comments

You can use reflection to extract the property names into a list

var foo = new test();
IList<string> properties = foo.GetType().GetProperties()
    .Select(p => p.Name).ToList();

OLD ANSWER

Try converting your object to a NameValueCollection (https://msdn.microsoft.com/en-us/library/system.collections.specialized.namevaluecollection(v=vs.110).aspx). This collection offers a count, and allows you hash table access to the values. You can also retrieve an IEnumerable for the Values (or Keys) to suit your needs.

var foo = new test();

NameValueCollection formFields = new NameValueCollection();
foo.GetType().GetProperties()
    .ToList()
    .ForEach(pi => formFields.Add(pi.Name, pi.GetValue(foo, null).ToString()));

NOTE: if .ToString() is too destructive, you can swap the NameValueCollection with the IDictionary implementation of your choice.

Code modified from this question: how to convert an instance of an anonymous type to a NameValueCollection

Community
  • 1
  • 1
Kevin Burdett
  • 2,892
  • 1
  • 12
  • 19
0

In the other class simply call test.ID = identification; identification being the variable that you would like ID to get the value from. test being your class name.

coderblogger
  • 84
  • 11
0

I believe you are looking for something like this (wrapped in example code).

class Program
{
    public class Test
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Address { get; set; }
    }

    static void Main(string[] args)
    {
        var propertyInfo = typeof(Test).GetProperties();
        var propertyCount = propertyInfo.Count();

        Console.WriteLine($"Property count is {propertyCount}");
        foreach (var info in propertyInfo)
        {
            Console.WriteLine($"Property Name: {info.Name}");
        }

        Console.ReadKey();
    }
}

Which would give you the output:

Code Output

This allows you to get the property count and property names of a class.