oleg-shilo / cs-script.core

.NET Core port of CS-Script
MIT License
100 stars 18 forks source link

Inherit partial class from interface issues #17

Closed LostSoulfly closed 3 years ago

LostSoulfly commented 3 years ago

Previously I only used an interface ISensor.cs and implemented it completely in each script I wrote but I wanted to clean things up and reduce script size so I created a base SensorBase.cs which uses ISensor.cs, both of which are contained in the PC2MQTT.Sensors namespace.

Somewhere I call:

this.sensor = CSScript.RoslynEvaluator
                    .ReferenceAssembliesFromCode(code)
                    .ReferenceAssembly(Assembly.GetExecutingAssembly())
                    .ReferenceAssembly(Assembly.GetExecutingAssembly().Location)
                    .LoadCode<ISensor>(code);

SensorBase.cs:

namespace PC2MQTT.Sensors
{
    public partial class SensorBase : ISensor
    {
        // A very bad logging library. BadLogger is the namespace.
        internal BadLogger.BadLogger Log;
    }
}

Here is the script I load with cs-script. There is more in the file, but the Log object is what I need to access from the SensorBase.cs class. I will override any methods I do not want the default implementation of here from SensorBase.

Example.cs script loaded by cs-script code above:

public class Example : SensorBase, ISensor
{
    // error CS0103: The name 'Log' does not exist in the current context
}

But in the Example script it is not able to find the Log object from SensorBase.cs. Is there a way for me to reference Log object from SensorBase.cs in my scripts?

oleg-shilo commented 3 years ago

you need to make Log field public. internal is problematic since SensorBase and Example are implemented in different scripts/assemblies

LostSoulfly commented 3 years ago

That seems to have solved it, thank you :)