Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Reflection Example
using System;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
interface IRessetable
{
void Reset();
}
class CredentialStorage: IRessetable
{
public void Reset()
{
Console.WriteLine("Executing CredentialStorage.Reset");
}
}
class FavoriteStorage: IRessetable
{
public void Reset()
{
Console.WriteLine("Executing FavoriteStorage.Reset");
}
}
class SettingsStorage: IRessetable
{
public void Reset()
{
Console.WriteLine("Executing SettingsStorage.Reset");
}
}
class UserSession: IRessetable
{
public CredentialStorage credentialStorage;
public FavoriteStorage favoriteStorage;
public SettingsStorage settingsStorage;
public bool test;
public UserSession(
CredentialStorage cd,
FavoriteStorage fs,
SettingsStorage ss
) {
this.credentialStorage = cd;
this.favoriteStorage = fs;
this.settingsStorage = ss;
this.test = false;
}
public void Reset()
{
ResettableProps().ForEach(x => x.Reset());
}
private List<IRessetable> ResettableProps()
{
return this
.GetType()
.GetFields()
.Select(x => x.GetValue(this))
.AsQueryable()
.OfType<IRessetable>()
.Cast<IRessetable>()
.ToList();
}
}
class Activity
{
static void Main(string[] args)
{
UserSession session = new UserSession(
new CredentialStorage(),
new FavoriteStorage(),
new SettingsStorage()
);
session.Reset();
}
}
/* Output:
Executing CredentialStorage.reset
Executing FavoriteStorage.reset
Executing SettingsStorage.reset
*/
import kotlin.reflect.full.memberProperties
import kotlin.reflect.jvm.*
import kotlin.reflect.jvm.internal.*
interface Resettable {
fun reset()
}
class CredentialStorage : Resettable {
override fun reset() {
println("Executing CredentialStorage.reset")
}
}
class FavoriteStorage : Resettable {
override fun reset() {
println("Executing FavoriteStorage.reset")
}
}
class SettingsStorage : Resettable {
override fun reset() {
println("Executing SettingsStorage.reset")
}
}
class UserSession(
val credentials: CredentialStorage,
val favorites: FavoriteStorage,
val settings: SettingsStorage
) : Resettable {
override fun reset() {
resettableProps().forEach { it.reset() }
}
fun resettableProps(): List<Resettable> {
return UserSession::class.memberProperties
.map { it.get(this) }
.filterIsInstance<Resettable>()
}
}
val session = UserSession(
CredentialStorage(),
FavoriteStorage(),
SettingsStorage()
)
println(session.reset())
/* Output:
Executing CredentialStorage.reset
Executing FavoriteStorage.reset
Executing SettingsStorage.reset
*/
// https://www.swiftbysundell.com/posts/reflection-in-swift
class CredentialStorage {}
class FavoriteStorage {}
class SettingsStorage {}
protocol Resettable {
func reset()
}
extension CredentialStorage: Resettable {
func reset() {
print("Executing CredentialStorage.reset")
}
}
extension FavoriteStorage: Resettable {
func reset() {
print("Executing FavoriteStorage.reset")
}
}
extension SettingsStorage: Resettable {
func reset() {
print("Executing SettingsStorage.reset")
}
}
extension Mirror {
func findProperties<T>(ofType type: T.Type) -> [T] {
return children.compactMap({ $0.value as? T })
}
}
class UserSession: Resettable {
let credentials: CredentialStorage
let favorites: FavoriteStorage
let settings: SettingsStorage
init(credentials: CredentialStorage, favorites: FavoriteStorage, settings: SettingsStorage) {
self.credentials = credentials
self.favorites = favorites
self.settings = settings
}
private var resettableProps: [Resettable] {
return Mirror(reflecting: self).findProperties(ofType: Resettable.self)
}
func reset() {
resettableProps.resetEach()
}
}
extension Array where Element == Resettable {
func resetEach() {
forEach({ $0.reset() })
}
}
let session = UserSession(
credentials: CredentialStorage(),
favorites: FavoriteStorage(),
settings: SettingsStorage()
)
session.reset()
/* Output:
Executing CredentialStorage.reset
Executing FavoriteStorage.reset
Executing SettingsStorage.reset
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment