C# Iterate through Class properties
You could possibly use Reflection to do this. As far as I understand it, you could enumerate the properties of your class and set the values. You would have to try this out and make sure you understand the order of the properties though. Refer to this MSDN Documentation for more information on this approach.
For a hint, you could possibly do something like:
Record record = new Record();
PropertyInfo[] properties = typeof(Record).GetProperties();
foreach (PropertyInfo property in properties)
{
property.SetValue(record, value);
}
Where value
is the value you're wanting to write in (so from your resultItems
array).
How to loop through all the properties of a class?
Use Reflection:
Type type = obj.GetType();
PropertyInfo[] properties = type.GetProperties();
foreach (PropertyInfo property in properties)
{
Console.WriteLine("Name: " + property.Name + ", Value: " + property.GetValue(obj, null));
}
for Excel - what tools/reference item must be added to gain access to BindingFlags, as there is no "System.Reflection" entry in the list
Edit: You can also specify a BindingFlags value to type.GetProperties()
:
BindingFlags flags = BindingFlags.Public | BindingFlags.Instance;
PropertyInfo[] properties = type.GetProperties(flags);
That will restrict the returned properties to public instance properties (excluding static properties, protected properties, etc).
You don't need to specify BindingFlags.GetProperty
, you use that when calling type.InvokeMember()
to get the value of a property.
How to loop through the properties of a Class?
Something like:
object obj = new object();
PropertyInfo[] properties = obj.GetType().GetProperties();
foreach (var p in properties)
{
var myVal = p.GetValue(obj);
}
Note you need to allocate the object and pass it into the PropertyInfo.
Iterate through methods and properties of an ES6 class
The constructor
and any defined methods are non-enumerable properties of the class's prototype
object.
You can therefore get an array of the names (without constructing an instance of the class) with:
Object.getOwnPropertyNames(MyClass.prototype)
You cannot obtain the properties without creating an instance, but having done so you can use the Object.keys
function which returns only the enumerable properties of an object:
Object.keys(myInstance)
AFAIK there's no standard way to obtain both the non-enumerable properties from the prototype and the enumerable properties of the instance together.
Looping through an object's (class instance) properties in python and printing them
Try not using private __dict__
,
you have python built-in function called vars()
def print_properties(self):
for prop, value in vars(self).items():
print(prop, ":", value) # or use format
Explantion of vars, from pythom offical docs:
Return the
__dict__
attribute for a module, class, instance, or any
other object with a__dict__
attribute.Objects such as modules and instances have an updateable
__dict__
attribute; however, other objects may have write restrictions on their
__dict__
attributes (for example, new-style classes use a dictproxy to prevent direct dictionary updates).Without an argument,
vars()
acts likelocals()
. Note, the locals
dictionary is only useful for reads since updates to the locals
dictionary are ignored.
Related Topics
Open a Folder Using Process.Start
Blocking Access to Private Member Variables? Force Use of Public Properties
How to Interact with Windows Media Player in C#
How to Highlight Wrapped Text in a Control Using the Graphics
Httpcontext.Current Is Null When Unit Test
How to Click a Button in a Webbrowser Control
Auto-Resize Multiple Forms Rendered on Panel
What Does Linq Return When the Results Are Empty
How to Use Async to Increase Winforms Performance
How to Click on the Radio Button Through the Element Id Attribute Using Selenium and C#
How to Create "Embedded" SQL 2008 Database File If It Doesn't Exist
Combining Datatemplates at Runtime
Getting Content/Message from Httpresponsemessage
Using Iconfiguration in C# Class Library