2024-12-16 01:34:28 +00:00
# Platform Reflection Quick Start Guide
2024-12-21 18:01:22 +00:00
This guide covers the essential steps to get started with Platform Reflection, a drop-in replacement for dart:mirrors with AOT compilation support.
2024-12-16 01:34:28 +00:00
## Installation
2024-12-21 18:01:22 +00:00
Add this to your package's `pubspec.yaml` file:
2024-12-16 01:34:28 +00:00
```yaml
dependencies:
platform_reflection: ^0.1.0
```
2024-12-21 18:01:22 +00:00
## Key Concepts
1. **Explicit Registration** : Unlike dart:mirrors, Platform Reflection requires explicit registration of classes and their members.
2. **AOT Compilation Support** : The registration process enables AOT compilation, which dart:mirrors doesn't support.
3. **API Similarity** : The reflection API is similar to dart:mirrors, easing the transition.
2024-12-16 01:34:28 +00:00
## Basic Usage
2024-12-21 18:01:22 +00:00
### 1. Define and Register a Class
2024-12-16 01:34:28 +00:00
```dart
2024-12-25 21:31:41 +00:00
import 'package:platform_mirrors/mirrors.dart';
2024-12-16 01:34:28 +00:00
@reflectable
class User {
String name;
int age;
2024-12-21 18:01:22 +00:00
2024-12-16 01:34:28 +00:00
User(this.name, this.age);
2024-12-21 18:01:22 +00:00
String greet() => "Hello, $name!";
2024-12-16 01:34:28 +00:00
}
2024-12-21 18:01:22 +00:00
void registerUser() {
2024-12-16 01:34:28 +00:00
Reflector.register(User);
Reflector.registerProperty(User, 'name', String);
Reflector.registerProperty(User, 'age', int);
2024-12-21 18:01:22 +00:00
Reflector.registerMethod(User, 'greet', [], false);
2024-12-16 01:34:28 +00:00
Reflector.registerConstructor(
User,
'',
parameterTypes: [String, int],
parameterNames: ['name', 'age'],
);
}
2024-12-21 18:01:22 +00:00
void main() {
registerUser();
// Your application code here
2024-12-16 01:34:28 +00:00
}
```
2024-12-21 18:01:22 +00:00
### 2. Use Reflection
2024-12-16 01:34:28 +00:00
```dart
2024-12-21 18:01:22 +00:00
void demonstrateReflection() {
final reflector = RuntimeReflector.instance;
2024-12-16 01:34:28 +00:00
2024-12-21 18:01:22 +00:00
// Create instance
final user = reflector.createInstance(
User,
positionalArgs: ['John Doe', 30],
) as User;
2024-12-16 01:34:28 +00:00
2024-12-21 18:01:22 +00:00
// Get mirror
final mirror = reflector.reflect(user);
2024-12-16 01:34:28 +00:00
2024-12-21 18:01:22 +00:00
// Property access and modification
print('Name: ${mirror.getField(const Symbol('name')).reflectee}');
mirror.setField(const Symbol('age'), 31);
2024-12-16 01:34:28 +00:00
2024-12-21 18:01:22 +00:00
// Method invocation
final greeting = mirror.invoke(const Symbol('greet'), []).reflectee as String;
print(greeting);
2024-12-16 01:34:28 +00:00
2024-12-21 18:01:22 +00:00
// Type information
final classMirror = reflector.reflectClass(User);
print('Type name: ${classMirror.name}');
2024-12-16 01:34:28 +00:00
}
```
2024-12-21 18:01:22 +00:00
## Key Differences from dart:mirrors
2024-12-16 01:34:28 +00:00
2024-12-21 18:01:22 +00:00
1. **Registration** : You must register classes, properties, methods, and constructors explicitly.
2. **Reflector Class** : Use the `Reflector` class for registration and the `RuntimeReflector` for reflection operations.
3. **Limited Automatic Discovery** : There's no automatic discovery of classes or members; everything must be registered.
2024-12-16 01:34:28 +00:00
## Best Practices
2024-12-21 18:01:22 +00:00
1. Register all reflectable types at application startup.
2. Cache instance mirrors for repeated operations on the same object.
3. Always handle potential `ReflectionException` s in your code.
4. Use reflection judiciously, as it can impact performance.
2024-12-16 01:34:28 +00:00
2024-12-21 18:01:22 +00:00
## Common Pitfalls
2024-12-16 01:34:28 +00:00
2024-12-21 18:01:22 +00:00
1. Forgetting to register a class or its members before reflection.
2. Attempting to reflect on private members (not supported).
3. Not handling potential exceptions during reflection operations.
2024-12-16 01:34:28 +00:00
## Next Steps
2024-12-21 18:01:22 +00:00
- Review the [API Reference ](../README.md#api-reference ) for detailed information.
- Explore the [examples ](../example ) directory for more complex scenarios.
- If migrating from dart:mirrors, check the [Migration Guide ](mirrors_comparison.md ) for a detailed comparison and transition tips.
Remember, Platform Reflection aims to provide the power of reflection with the benefits of AOT compilation. The trade-off is the need for explicit registration, which gives you more control over what can be reflected upon in your application.