Created
April 2, 2019 17:48
-
-
Save jonathan-beebe/b4a49202069472ec3e269faa8e0dd180 to your computer and use it in GitHub Desktop.
Reflection Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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