buunguyen / fasterflect

.NET Reflection Made Fast and Simple ⛺
https://www.nuget.org/packages/fasterflect/
Apache License 2.0
285 stars 57 forks source link

Modify/extend objects at runtime #24

Open bmmptlgc opened 1 year ago

bmmptlgc commented 1 year ago

I need modify/extend objects at runtime. For example,

I want to extend an Order object returned by an API, so that the serialized JSON response would go from:

{
    "orderId": "abc",
    "price": 100,
    "orderItems": [
        {
            "orderItemId": "def",
            "price": 55
        },
        {
            "orderItemId": "ghi",
            "price": 45
        }
    ]
}

to:

{
    "@id": "https://someurl/api/orders/abc",
    "orderId": "abc",
    "price": 100,
    "orderItems": [
        {
            "@id": "https://someurl/api/orders/abc/order-item/def",
            "orderItemId": "def",
            "price": 55
        },
        {
            "@id": "https://someurl/api/orders/abc/order-item/ghi",
            "orderItemId": "ghi",
            "price": 45
        }
    ]
}

I am able to accomplish this using reflection, but was wondering if I can simplify the code with fasterflect.

Essentially, I need to an Id field to OrderItem with a JsonPropertyName attribute with the value "@id". I need to do the same thing to the Order object, plus add a couple more fields I didn't include in the example above, for simplicity.

Can fasterflect do this?