Giter Club home page Giter Club logo

metaobject's Introduction

MetaObject

Install-Package MetaObject

Background

C# 4.0's new dynamic keyword brings a realm of new possibilities to C#.

If you inherit your class from DynamicObject, it becomes trivial to add support for dynamic methods, properties, indexes, and more.

As soon as you want to inherit your class from something else, however, it becomes a small nightmare ...

As much as I'd love to learn how to create expression trees in C#, what I really want to do is add dynamic functionality to my classes!

I started digging around and discovered that DynamicObject is basically an empty class that has a rock solid DynamicMetaObject implementation that delegates dynamic calls to methods on the DynamicObject, eg. TryInvokeMember

Unfortunately, this DynamicMetaObject implementation is private and sealed!

Thankfully, the DLR (which is where this class comes from) is open-source, released under the Apache 2.0 license!

I took the source code, dealt with some dependency issues, removed the dependency on a DynamicObject and ended up with MetaObject

Usage

Assuming you have a reference to MetaObject.dll or you included MetaObject.cs in your project:

using System;
using System.Dynamic;

public class MyClass : Whatever, IDynamicMetaObjectProvider {

    // This 1 line is *ALL* you need to add support for all of the DynamicObject methods
    public DynamicMetaObject GetMetaObject(System.Linq.Expressions.Expression e){ return new MetaObject(e, this); }

    // Now, if you want to handle dynamic method calls, you can implement TryInvokeMember, just like you would in DynamicObject!
    public bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result) {
        if (binder.Name.Contains("Cool")) {
            result = "You called a method with Cool in the name!";
            return true;
        } else {
            result = null;
            return false;
        }
    }
}

Now, if you want to invoke your dynamic methods:

// This will give you a compiler error because MyClass doesn't have a method called CoolMethod
var instance = new MyClass();
instance.CoolMethod();

// This will work and return "You called a method with Cool in the name!"
dynamic instance = new MyClass();
instance.CoolMethod();

All of the Try* methods that DynamicObject supports are supported by MetaObject:

  • TryBinaryOperation
  • TryConvert
  • TryGetIndex
  • TryGetMember
  • TryInvoke
  • TryInvokeMember
  • TrySetIndex
  • TrySetMember
  • TryUnaryOperation

metaobject's People

Stargazers

问道老王 avatar  avatar Teneko avatar  avatar Subrato avatar Rebecca Taylor avatar  avatar Jeremy Caney avatar Alexander avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.