Skip to content

A small library for manipulating properties via their names known only at runtime

License

Notifications You must be signed in to change notification settings

Molnix888/obj-props-dynamic-setter

Repository files navigation

ObjPropsDynamicSetter

Coverage Mutation Score

A small library providing object extensions for getting properties information and manipulating their values via property names known at runtime. Uses reflection under the hood. Might be useful for playing with models properties in data-driven testing, but not limited to it. Supports nested properties access and access to non-public properties.

Use

Simple usage examples:

var model = new Model();
var propertyName = "Foo";

// PropertyInfo retrieval from a property
var propertyInfo = model.GetPropertyInfo(propertyName);

// Property value retrieval
var propertyValue = model.GetPropertyValue(propertyName);

// Setting new value to a property
var value = 1000;
model.SetPropertyValue(propertyName, value);

Public properties are accessible by default. To access non-public ones, optional parameter 'includeNonPublic' should be set to 'true':

model.SetPropertyValue(propertyName, value, true);

To access nested property, full path should be specified with dot ('.') as delimiter:

public class Model
{
    public NestedModel NestedModel { get; set; }
}

public class NestedModel
{
    public int Bar { get; set; }
}

// Internal initialization logic skipped for simplicity
var model = new Model();
var propertyName = "NestedModel.Bar";

var value = model.GetPropertyValue(propertyName);

Download

The latest release is on NuGet and GitHub.

About

A small library for manipulating properties via their names known only at runtime

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors 4

  •  
  •  
  •  
  •  

Languages