scriptsharp icon indicating copy to clipboard operation
scriptsharp copied to clipboard

Some ideas for introspection and new features

Open danfma opened this issue 13 years ago • 1 comments

Sometimes we need to hack, even in C#, to get some dynamic processing. So I propose the following changes in the ScriptSharp compiler:

Make the Object class inherits of the IDictionary<string, Object> to allow us to simulate the introspection of properties and some dynamic features (this could also be allowed by just making a simple enumerable and indexable interface);

public static void Main() {
   Object a = new Object();
   a["myVariable"] = 10;

   // if the implementation uses a IEnumerable<string> for the properties
   foreach (string propertyName in a) {
      // do something
      // I am missing the Document.Write method, it exists?! heheh :P
   }

   // if the implementation uses the IDictionary<string,Object>
   foreach (KeyValuePair<string, Object> property in a) {
      // do something with the property.Key and property.Value
   }
}
// translation of the code
var a = {}; // or new Object() maybe
a['myVariable'] = 10;

// implementation 1
for (var propertyName in a) {
  // do something
}

// implementation 2
var $enum1 = ss.IDictionary.getDictionaryEnumerator(data);
while ($enum1.moveNext()) {
  var item = $enum1.current; // the item would have the properties: key and value
}

Would be nice to make the ScriptSharp compatible with the C# 3.5 compiler, so we could use the extension methods (in js, we could simulate that by changing the prototype of some objects or by calling a static method, just as the C# compiler does).

public static class ObjectExtensions
{
  public static int CalculateHash(this Object self) {
    // do the calculation and returns
    return 0;
  }
}

public static void Main() {
  Object a = new Object();
  int hashCode = a.CalculateHash();
}
// implementation 1
Object.calculateHash = function(self) {
  return 0;
}; // <-- I really miss the comma at the generated code by the ScriptSharp :D (and the VS with Resharper too)

var a = {};
var hashCode = Object.calculateHash(a);


// implementation 2
Object.prototype.calculateHash = function() {
  var self = this;
  return 0;
}; // <-- I really miss the comma at the generated code by the ScriptSharp :D (and the VS with Resharper too)

var a = {};
var hashCode = a.calculateHash();

If the .NET compiler is compatible with the C#4 version, then we could just translate almost directly the code from C# to JS

var myInt = 10;
myInt = myInt + 20;

dynamic someVar = myObject;
someVar.Property = 10;
someVar.Method();
var myInt = 10;
myInt = myInt + 20;

// ok, we will need a convension to how we will translate the properties and methods, 
var someVar = myObject;
someVar.property = 10;
someVar.method();

Remoting

That is more difficult, but I am thinking, as soon as I have some time, to make some tool that will make the bridge between the .NET backend and the ScriptSharp, transparently. Maybe with a code generator or something like that... the integration with the Asp.NET MVC would be more easily to make.

By reading the controller methods (actions) we could generate a proxy in the client with the same signature (that will be just an wrapper for the jQuery API to call the server). But, as long the default Javascript Serializer of the .NET framework is not good enough as the Json.NET, maybe some convertions would be necessary.

danfma avatar Dec 27 '11 03:12 danfma

I've determined that a cast from any custom object to object, then to dictionary will achieve the first functionality.


CustomFancyObject realObject=new CustomFancyObject();
Dictionary item=((Dictionary)((object)realObject));
item["foo"]=bar+1;

will compile to the ~correct javascript code.

As far as extension methods, since they are just syntax sugar I would think they would be trivial to implement, along with some of the other sugar (var, inline object instantiation).

dested avatar Jul 17 '12 02:07 dested