Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to traverse class properties for Nunit tests?

How to traverse class properties for Nunit tests?

Scheduled Pinned Locked Moved C#
csharpdata-structurestestingbeta-testingtutorial
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pankazmittal
    wrote on last edited by
    #1

    Hi I am working on a project in which I need to NUnit testing. To do that I need to write tests for each class. I am trying to automate that process. 1. How to know names of public properties of class in .Net dynamically? 2. I am also looking for property types to set some default values. 3. There might be nested classes also. I am wondering how to traverse that nested tree? Please advice. Thanks Pankaj

    G B E 3 Replies Last reply
    0
    • P pankazmittal

      Hi I am working on a project in which I need to NUnit testing. To do that I need to write tests for each class. I am trying to automate that process. 1. How to know names of public properties of class in .Net dynamically? 2. I am also looking for property types to set some default values. 3. There might be nested classes also. I am wondering how to traverse that nested tree? Please advice. Thanks Pankaj

      G Offline
      G Offline
      Gareth H
      wrote on last edited by
      #2

      pankazmittal, To get the names dynamically, you need to use Reflection. http://msdn.microsoft.com/en-us/library/system.reflection(VS.80).aspx[^] Regards, Gareth.

      1 Reply Last reply
      0
      • P pankazmittal

        Hi I am working on a project in which I need to NUnit testing. To do that I need to write tests for each class. I am trying to automate that process. 1. How to know names of public properties of class in .Net dynamically? 2. I am also looking for property types to set some default values. 3. There might be nested classes also. I am wondering how to traverse that nested tree? Please advice. Thanks Pankaj

        B Offline
        B Offline
        Brady Kelly
        wrote on last edited by
        #3

        I'm sure if you look around you'll find something that already exists to do this kind of job.

        1 Reply Last reply
        0
        • P pankazmittal

          Hi I am working on a project in which I need to NUnit testing. To do that I need to write tests for each class. I am trying to automate that process. 1. How to know names of public properties of class in .Net dynamically? 2. I am also looking for property types to set some default values. 3. There might be nested classes also. I am wondering how to traverse that nested tree? Please advice. Thanks Pankaj

          E Offline
          E Offline
          Ed Poore
          wrote on last edited by
          #4

          Using LINQ because it's so much easier... :cool: 1) How to know names of public properties of classes in .NET dynamically?

          var properties = yourType.GetProperties().Select(p => p.Name); // Returns an IEnumerable<string> of the names (only returns public by default)

          1. Property types

          var properties = yourType.GetProperties().Select(p => p.PropertyType); // Returns IEnumerable<Type> of property type
          var instance = Activator.CreateInstance(properties.First()); // Creates the default value for the type of the first property in list

          1. Nested classes

          var childClasses = typeof(Program).GetNestedTypes().Select(t => t.Name); // Returns IEnumerable<string> of names of child properties

          Note you can retrieve these without LINQ just forget the .Select(...) pattern, but you asked for names which implies strings so I used LINQ to extract the names rather than a loop.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups