CS-Script Hosting in LinqPad

190 views
Skip to first unread message

rporo...@gmail.com

unread,
Jul 27, 2012, 9:33:11 AM7/27/12
to cs-s...@googlegroups.com
I was first playing around with CS-Script in LinqPad (it's my scratchpad of choice) and encountered an exception that didn't make sense. So I thought I'd throw it in here to see if there's a behind-the-scenes explanation for it. :) Up front, I would have expected Simplified Hosting Model to cover the scenario the exception seems to be thrown for (hope that sentence made sense).

Using the CS-Script binaries pulled on 2012-07-26. Targeting .NET Framework v3.5 (though I have up through v4.0 installed as well). LinqPad v4.42.01 operating in C# Program mode. Code is:

void Main()
{
  var addScript = CSScript.LoadCode(
                  @"
                  using System;
                 
                  public class Script
                  {
                    public int Add(int a, int b)
                    {
                        return a + b;
                    }
                  }
                  ")
                  .CreateInstance("Script")
                  .AlignToInterface<ICalc>();
  addScript.Add(1, 2).Dump();
}

public interface ICalc
{
  int Add(int a, int b);
}

The exception I'm getting is

Message: Error compiling proxy class:
15 CS0012 The type 'LINQPad.User.TypedDataContext' is defined in an assembly that is not referenced. You must add a reference to assembly 'TypedDataContext_vphdoz, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.

Source: CSScriptLibrary.v3.5

StackTrace:
  at CSScriptLibrary.ThirdpartyLibraries.Rubenhak.Utils.ObjectCaster`1.BuildProxyClass(Type interfaceType, Type sourceType, String[] refAssemblies)
  at CSScriptLibrary.ThirdpartyLibraries.Rubenhak.Utils.ObjectCaster`1.CastToInterface(Type interfaceType, Object o, String[] refAssemblies)
  at CSScriptLibrary.ThirdpartyLibraries.Rubenhak.Utils.ObjectCaster`1.As(Object o, String[] refAssemblies)
  at CSScriptLibraryExtensionMethods.TryAlignToInterface[T](Object obj)
  at CSScriptLibraryExtensionMethods.AlignToInterface[T](Object obj)


Oleg Shilo

unread,
Jul 28, 2012, 4:12:34 AM7/28/12
to cs-s...@googlegroups.com, rporo...@gmail.com
It is hard to speculate what happens in LinqPad but it works OK outside of LinqPad. I have attached the VS2008 project with the working sample based on your code. Please note that because Imy environment configured for .NET4.0 I had to provide the routine for the compiler creation directly in the host application to avoid any compiler version conflict.

Oleg 
Script.7z

rporo...@gmail.com

unread,
Jul 28, 2012, 8:03:18 AM7/28/12
to cs-s...@googlegroups.com, rporo...@gmail.com
Using a VS2010 project to understand the basics of interface alignment is what I did. LinqPad worked for script loading/testing, but the combination had a problem with referencing assemblies otherwise. I was hoping you'd encountered the situation before and had a work-around.

It was mainly disappointing because I use LinqPad for all my throw-away coding (like this initial testing to insure I understand what's what).

Oleg Shilo

unread,
Jul 28, 2012, 8:56:05 AM7/28/12
to cs-s...@googlegroups.com
I just downloaded Lonqpad 4, added CSScriptLibrary.3.5.dll and required namespaces and it works just fine with the script code below. 

Thus none of the tools you are using seems to be a problem.

        void Main()
{
var addScript = CSScript.LoadCode(
@"using System;
 
 public class Script
 {
public int Add(int a, int b)
{
return a + b;
}
 }
 ")
.CreateInstance("Script")
.AlignToInterface<ICalc>();

addScript.Add(1, 3).Dump("Result: ");
}

public interface ICalc
{
int Add(int a, int b);
}
Reply all
Reply to author
Forward
0 new messages