Comments (6)
Shit. If without this or this, this can't be implemented nicely with SourceGenerator.
from kifanet.
So here is a slight better solution without needing the Source Generator magic:
public static class Safe {
public static T Get<T>(T? value) {
if (value != null) {
return value;
}
var method = new StackFrame(1).GetMethod();
if (method == null) { // This if may be skipped IMO
throw new NullReferenceException(
"Unexpectedly failed to find info of getting a null value.");
}
throw new NullReferenceException(
$"Property {method.Name[4..]} of class {method.DeclaringType} is expected to be non-null, but is actually null.");
}
public static void Set<T>(ref T field, T? value) {
if (value == null) {
var method = new StackFrame(1).GetMethod();
if (method == null) { // This if may be skipped IMO
throw new NullReferenceException(
"Unexpectedly failed to find info of setting a null value.");
}
throw new NullReferenceException(
$"Cannot assign null value to non-null property {method.Name[4..]} of class {method.DeclaringType}.");
}
field = value;
}
}
And the usage looks like:
private string? _id;
public string Id {
get => Safe.Get(_id);
set => Safe.Set(ref _id, value);
}
It's also possible to implement the Set
method to return instead of ref
.
from kifanet.
For struct types like int to work. You may need to add
public static T Get<T>(T? value) where T : struct {
if (value != null) {
return value.Value;
}
var method = new StackFrame(1).GetMethod();
if (method == null) {
throw new NullReferenceException(
"Unexpectedly failed to find info of getting a null value.");
}
throw new NullReferenceException(
$"Property {method.Name[4..]} of class {method.DeclaringType} is expected to be non-null, but is actually null.");
}
where the where T : struct
is the key.
from kifanet.
Performance wise, only failure handling is impacted by reflection. So it should be OK. Especially errors like this should be fixed instead of tolerated, so most likely it should only happen once in each run and should be in common in developing time.
from kifanet.
As suggested by this answer, [CallerMemberName]
will help a lot, but there is no [CallerTypeName]
is not available (see here).
Since I don't feel file path is a reasonable solution, we will stick to StackFrame
approach for now.
from kifanet.
This change conflicts with Json.net. Fixed in 9ade493
In general, I think this is solved.
from kifanet.
Related Issues (12)
- Reimplement all services in C#
- Create a shared ffmpeg module and/or binary execution module HOT 1
- Deprecate usage of KifaFileCommand
- Cleanup NLog related configs/setups
- Implement swisscom find accounts in server side
- Support bilibili playlist download
- Support extracting audios (from bilibili videos) HOT 1
- Integrate biaori data
- Add Overrides support for DataModel
- Create DataModel for MemriseWord HOT 1
- Find a better way to initialize ServiceClient
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from kifanet.