Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

『dynamicの内部実装』の動的コード生成に関する説明について #319

Open
toonmoondaemon opened this issue Sep 16, 2020 · 0 comments

Comments

@toonmoondaemon
Copy link

dynamicの内部実装CallSite.Target 内での動的コード生成の説明で、

その結果、Target が以下のような状態に更新されます。
(中略)
ここで、((Point)x).X の部分を生成するのが CallSiteBinder の役目です。

とありますが、実際にBindメソッドが生成を担うのはif (x is Point) return ((Point)x).X;の部分ではないでしょうか?

雑実装ですが、以下のようにif、is、returnも書く感じになるかと思います。

public class PropertyXBinder : CallSiteBinder
{
    public override Expression Bind(object[] args, ReadOnlyCollection<ParameterExpression> parameters, LabelTarget returnLabel)
    {
        var arg = args[0];
        var argType = arg.GetType();
        var property = argType.GetProperty("X");

        var parameter = parameters[0];

        return Expression.IfThen(
            Expression.TypeIs(
                parameter,
                argType
                ),
            Expression.Return(
                returnLabel,
                Expression.Property(
                    Expression.Convert(
                        parameter,
                        argType
                        ),
                    property
                    )
                )
            );
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant