How can I access Layerstates in C#?

How can I access Layerstates in C#? I would like to find all LayerStates and if found, i want to activate a certain one. I know the LayerStateManager is part of a plugin called Rhino Bonus Tools. I can find that plugin and find the commands:

        Guid pluginGuid = Rhino.PlugIns.PlugIn.IdFromName("Rhino Bonus Tools");
        object BonusTools = Rhino.RhinoApp.GetPlugInObject(pluginGuid);
        string[] commands = Rhino.PlugIns.PlugIn.GetEnglishCommandNames(pluginGuid);

but how can I get the LayerStateNames?
I did this in Python before, ( but i am quite new to C#)
def checkLayerStates(viewName):
objPlugIn = rc.RhinoApp.GetPlugInObject(“Rhino Bonus Tools”)
arrStates = objPlugIn.LayerStateNames()
if len(arrStates) > 0:
for strState in arrStates:
if strState == viewName:
print "restoring LayerState: " + strState

Hi Ralph,

The object returned by Rhino.RhinoApp.GetPlugInObject is a COM object, not a .NET object. Thus, you must treat the object somewhat differently.

C# 4 introduced the new dynamic keyword, which was designed with COM Interop in mind. The intent was to make calling properties and methods in C# as easy as it was in VBScript, Visual Basic, etc.

That said, you can call functions on the Rhino Bonus Tools object, which contains methods that access the Layer State Manager, like this:

Guid plugin_id = Rhino.PlugIns.PlugIn.IdFromName("Rhino Bonus Tools");
if (plugin_id == Guid.Empty)
  return Result.Failure;

// Magic blue keyword...
dynamic plugin_obj = Rhino.RhinoApp.GetPlugInObject(plugin_id);
if (null == plugin_obj)
  return Result.Failure;

object[] layer_states = plugin_obj.LayerStateNames();
foreach (object layer_state in layer_states)
  string state_name = layer_state.ToString();
  if (state_name.Equals("MyLayerState", StringComparison.OrdinalIgnoreCase))
    plugin_obj.RestoreLayerState(layer_state, 0);

– Dale

Hi Dale - Thank you very much, now i can go on