Skip to content

Commit f8d2ee6

Browse files
committed
Added extension to get property names from an object
1 parent 93cee79 commit f8d2ee6

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/MADE.Runtime/Extensions/ReflectionExtensions.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace MADE.Runtime.Extensions
22
{
33
using System;
4+
using System.Collections.Generic;
45
using System.Reflection;
56

67
/// <summary>
@@ -23,5 +24,19 @@ public static T GetPropertyValue<T>(this object obj, string property)
2324
PropertyInfo prop = type.GetProperty(property);
2425
return prop?.GetValue(obj) as T;
2526
}
27+
28+
/// <summary>
29+
/// Gets all the property names declared for the specified object.
30+
/// </summary>
31+
/// <param name="obj">The object to retrieve property names from.</param>
32+
/// <returns>A collection of object property names as a string.</returns>
33+
public static IEnumerable<string> GetPropertyNames(this object obj)
34+
{
35+
Type type = obj.GetType();
36+
foreach (PropertyInfo property in type.GetTypeInfo().DeclaredProperties)
37+
{
38+
yield return property.Name;
39+
}
40+
}
2641
}
2742
}

0 commit comments

Comments
 (0)