Announcement

Collapse
No announcement yet.

C# Script Question

Collapse
X
 
  • 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.


    Code:
    //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");
    	hs.WriteLog("CustomFunction",DateTime.Now.ToString());
            DailyInformation di = new DailyInformation(DateTime.Now);
    	hs.WriteLog("CustomFunction",di.ToString());
    	hs.WriteLog("CustomFunction","After Creation of new DailyInformation Object");
    	return ;
    }
    public class DailyInformation
        {
    //blah blah blah...
    }
    Thoughts?

    #2
    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 http://www.csscript.net/ 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#...

    Thanks




    Code:
    //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

    Comment


      #3
      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......

      Comment


        #4
        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:

        http://board.homeseer.com/showthread.php?t=178122

        I hope this can help you.

        Pablo.

        Comment


          #5
          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
          MyCalcFunctions.DailyInformation
          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.

          Comment


            #6
            Bixter,
            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.


            Code:
            Object myvar = new Object();
                        System.Reflection.PropertyInfo [] properties = di.GetType().GetProperties();
                        foreach(System.Reflection.PropertyInfo property in properties)
                        {
                            try 
                            {
                                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));
                            hs.CounterReset(property.Name);

            Comment


              #7
              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.

              Comment


                #8
                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.

                Comment

                Working...
                X