No announcement yet.

C# Script Question

  • Filter
  • Time
  • Show
Clear All
new posts

    C# Script Question

    So after sort of giving up on importing an and using an external dll that I had created I decided to try and compile the classes that I needed in my c# script. I was able to get everything compiled through trial and error but have been unable to create a class object that is i created within the script. The error message is as shown.
    If I do not call the class, the function works as expected writing to the log.
    I assume this means I cannot declare a class within a C# Script? What are my options? Are there anyworkarounds?

    As an aside, I referenced dll's the way I understood it from the documentation. Is this correct? Using crashes the compiler...

    Oct-07 3:34:54 PM Error 1 Running script more info: Object reference not set to an instance of an object
    Oct-07 3:34:54 PM Error 1 Running script /usr/local/HomeSeer/scripts/MyFunctions.cs :Exception has been thrown by the target of an invocation.

    //css_reference System.Data.dll;
    //css_reference /usr/lib/mono/4.5/mscorlib.dll;
    public void Main(object[] param) {
    	hs.WriteLog("CustomFunction","Before Creation of new DailyInformation Object");
            DailyInformation di = new DailyInformation(DateTime.Now);
    	hs.WriteLog("CustomFunction","After Creation of new DailyInformation Object");
    	return ;
    public class DailyInformation
    //blah blah blah...

    So I've tried a bunch of other stuff but nothing seems to work.
    Based on some other posts I spent extensive time looking at which seems to be the compiler being used.

    In their examples it seems simple to create classes and functions within scripts.

    To date, I have not been successful at either including other functions or referencing outside scripts or dlls'. I have tried different types of directives as shown below (not all at the same time) but nothing seems to be included, i have no way of even telling if the directives are being picked up.

    Has anyone had any experience with running C# on Linux referencing other namespaces in cs files or external dll's? Care to share a sample script?

    Is it documented anywhere how HomeSeer customizes/wraps their implementation using csscript?

    At this point I'm running out of faith in using C#...


    //css_searchdir /usr/local/HomeSeer/scripts;
    //css_import MyCalcFunctions;
    //css_reference MyCalcFunctions.dll; 
    public Object Main(Object[] parm) {	
    	hs.WriteLog("CustomFunction","Before Creation of new MyCalcFunctions.DailyInformation Object");
    	MyCalcFunctions.DailyInformation di = new MyCalcFunctions.DailyInformation();
    	hs.WriteLog("CustomFunction","After Creation of new DailyInformation Object");
    	return null;
    Compiling script /usr/local/HomeSeer/scripts/Test2.cs: {interactive}(23,9): error CS0246: The type or namespace name `MyCalcFunctions' could not be found. Are you missing a using directive or an assembly reference? {interactive}(7,17): warning CS0414: The private field `Script.hs' is assigned but its value is never used


      It is a fruitless pursuit... at least at current.

      I'm working with HomeSeer's developers to fix the bug, but it has been slow moving. So far, if you read some of our other threads, we have determined that functions and parameters do not work in C#. Even using a void main there tends to be memory leak errors.

      This is all interesting considering the documentation blatantly states that CS is more efficient in Linux. It is likely nothing to do with your script. A Class is just a more complex function. If a basic function doesn't work......


        Hi, my recommendation, IMHO, is to work with HS3 Plugins... I'd got crazy trying to use references to DLLs and after a lot of nights I gave up with this technique, but with Plugins everything works fine...

        There are some interesting posts inhere that I recommend to entering in this new world (HS3 Plugins)... The best one for me was:

        I hope this can help you.



          I use c# all the time and love it (not a VB guy). You have to use full names for your types so if the names space for MyCalcFunctions.dll is MyCalcFunctions then you have the right
          I don't see a using though like it is complaining about. Top of the file should have
          using MyCalcFunctions;

          That should do it. Make sure "MyCalcFunctions.DailyInformation" is indeed the full name.


            Thanks for your hints. Turns out, anything that is not in the standard libraries and needs to be imported needs to have the full name. Using that, I was able to compile my script using my custom dll in c#. Notice I had to do it even for System.Reflection classes.

            Thanks for your assistance, I'm happy to get this sorted out.

            Object myvar = new Object();
                        System.Reflection.PropertyInfo [] properties = di.GetType().GetProperties();
                        foreach(System.Reflection.PropertyInfo property in properties)
                                if (Startup)
                                    errst = hs.CreateVar(property.Name);
                                myvar = hs.GetVar(property.Name);
                                if(myvar.ToString() == ("A call was made to retrieve a global variable (" + property.Name + ") but that global variable has not been defined."))
                                    errst = hs.CreateVar(property.Name);
                            catch( Exception ex)
                                if (ex.Message == "Object reference not set to an instance of an object")
                                    errst = hs.CreateVar(property.Name);
                            errst = hs.SaveVar(property.Name, property.GetValue(di, null));


              Have you ever tried to run a .cs script on a Linux version of HS? I can't get it to run, even with a "null" script: Object not set to an instance of an object. HS is running fine (although there is a problem when HS tries to unzip plug-ins: they have to be manually unzipped first). Mono is installed (or HS wouldn't work).

              Here is the entire script:

              public Object Main(Object[] parm) {

              return 0;


              Result in log file:
              C# script exception(0), re-starting: Object reference not set to an instance of an object

              Thanks for any advise you can provide.


                I ran into similar problem. How do you define a class in a C# script and use it within the same script? I get the same Namespace compilation error that was mentioned earlier.