nikhilk/scriptsharp — Issue #135: Some ideas for introspection and new features
Author: danfma
Created: 2011-12-27 03:45:15+00:00
State: open

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 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);

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

// if the implementation uses a IEnumerable 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
foreach (KeyValuePair property in a) {
// do something with the property.Key and property.Value
}
}
```

```javascript
// 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).

```c#
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();
}
```

```javascript
// 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

```c#
var myInt = 10;
myInt = myInt + 20;

dynamic someVar = myObject;
someVar.Property = 10;
someVar.Method();
```

```javascript
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.