Problem Using C#-defined Custom Attributes in JScript.NET

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Zach Mortensen

    Problem Using C#-defined Custom Attributes in JScript.NET

    I can't seem to get dynamically-compiled JScript code to use
    C#-defined custom attributes. I have a simple attribute and a class
    defined in a C# assembly:

    namespace MyNamespace
    {
    [AttributeUsage( AttributeTarget s.All)]
    public abstract class MyCsharpAttribu te : Attribute
    {
    }

    [AttributeUsage( AttributeTarget s.Class)]
    public class MyCsharpClassAt tribute : MyCsharpAttribu te
    {
    }

    public class MyCsClass
    {
    }
    }

    Needless to say, I have many, many C# classes that use these
    attributes without any problem whatsoever.

    I also have some dynamically-compiled JScript.NET code (called from
    C#) that needs to use these attributes. Whenever I attempt to use the
    attributes within the JScript.NET, the JScript does not compile. I
    receive the error "Unknown custom attribute class or constructor"

    // This class compiles properly, there is no problem referencing the
    assembly
    // that contains MyCsharpClass and MyCsharpClassAt tribute.

    public class MyJsClass extends MyNamespace.MyC sClass {
    }

    public AttributeUsage( AttributeTarget s.Class) class
    MyJscriptClassA ttribute extends MyNamespace.MyC sharpClassAttri bute {
    public function MyJscriptClassA ttribute(name) {
    }
    }

    public MyJscriptClassA ttribute("Hello , world.")
    class MyOtherJsClass extends MyNamespace.MyC sClass {
    }

    Note that I get the same error when I use the MyCsharpClassAt tribute
    directly in place of MyJscriptClassA ttribute. I also get the same
    error if MyJscriptClassA ttribute extends MyCsharpAttribu te instead of
    MyCsharpClassAt tribute.

    Here's the catch: if I change MyJscriptClassA ttribute so that it
    extends Attribute instead of MyCsharpClassAt tribute (i.e. remove C#
    attributes from the equation and define the MyJscriptClassA ttribute
    strictly in JScript), the JScript.NET code compiles properly!

    Can anyone shed any light on this problem? At this point, I think it
    must be a problem with the JScript.NET compiler. Thanks in advance.
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Problem Using C#-defined Custom Attributes in JScript.NET

    Zach,

    Are you sure that you are setting a reference to the assembly that
    contains the attribute correctly? It seems like this could be it to me. If
    not, then are you sure that the namespace resolution is working correctly
    (some sort of using statement for JS that you are forgetting, so that it
    isn't finding it?).

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Zach Mortensen" <zmortensen@hot mail.com> wrote in message
    news:c98f734c.0 312230841.73b43 7a7@posting.goo gle.com...[color=blue]
    > I can't seem to get dynamically-compiled JScript code to use
    > C#-defined custom attributes. I have a simple attribute and a class
    > defined in a C# assembly:
    >
    > namespace MyNamespace
    > {
    > [AttributeUsage( AttributeTarget s.All)]
    > public abstract class MyCsharpAttribu te : Attribute
    > {
    > }
    >
    > [AttributeUsage( AttributeTarget s.Class)]
    > public class MyCsharpClassAt tribute : MyCsharpAttribu te
    > {
    > }
    >
    > public class MyCsClass
    > {
    > }
    > }
    >
    > Needless to say, I have many, many C# classes that use these
    > attributes without any problem whatsoever.
    >
    > I also have some dynamically-compiled JScript.NET code (called from
    > C#) that needs to use these attributes. Whenever I attempt to use the
    > attributes within the JScript.NET, the JScript does not compile. I
    > receive the error "Unknown custom attribute class or constructor"
    >
    > // This class compiles properly, there is no problem referencing the
    > assembly
    > // that contains MyCsharpClass and MyCsharpClassAt tribute.
    >
    > public class MyJsClass extends MyNamespace.MyC sClass {
    > }
    >
    > public AttributeUsage( AttributeTarget s.Class) class
    > MyJscriptClassA ttribute extends MyNamespace.MyC sharpClassAttri bute {
    > public function MyJscriptClassA ttribute(name) {
    > }
    > }
    >
    > public MyJscriptClassA ttribute("Hello , world.")
    > class MyOtherJsClass extends MyNamespace.MyC sClass {
    > }
    >
    > Note that I get the same error when I use the MyCsharpClassAt tribute
    > directly in place of MyJscriptClassA ttribute. I also get the same
    > error if MyJscriptClassA ttribute extends MyCsharpAttribu te instead of
    > MyCsharpClassAt tribute.
    >
    > Here's the catch: if I change MyJscriptClassA ttribute so that it
    > extends Attribute instead of MyCsharpClassAt tribute (i.e. remove C#
    > attributes from the equation and define the MyJscriptClassA ttribute
    > strictly in JScript), the JScript.NET code compiles properly!
    >
    > Can anyone shed any light on this problem? At this point, I think it
    > must be a problem with the JScript.NET compiler. Thanks in advance.[/color]


    Comment

    • Zach Mortensen

      #3
      Re: Problem Using C#-defined Custom Attributes in JScript.NET

      Thanks for the reply. I am sure that all of the assembly-references
      and using-statements are correct because I can derive a JScript class
      from a C# class that is defined in the same assembly as the C#-defined
      custom attribute that causes the problem, and if I omit the attributes
      from the JScript code, the code compiles properly. The code also
      compiles if I change the attribute so that it extends System.Attribut e
      rather than my custom attribute. Please see the example JScript code
      that I posted earlier.

      "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in message news:<uXHXkXXyD HA.3224@tk2msft ngp13.phx.gbl>. ..[color=blue]
      > Zach,
      >
      > Are you sure that you are setting a reference to the assembly that
      > contains the attribute correctly? It seems like this could be it to me. If
      > not, then are you sure that the namespace resolution is working correctly
      > (some sort of using statement for JS that you are forgetting, so that it
      > isn't finding it?).
      >
      > Hope this helps.[/color]

      Comment

      Working...