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

Z

Zach Mortensen

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(AttributeTargets.All)]
public abstract class MyCsharpAttribute : Attribute
{
}

[AttributeUsage(AttributeTargets.Class)]
public class MyCsharpClassAttribute : MyCsharpAttribute
{
}

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

public class MyJsClass extends MyNamespace.MyCsClass {
}

public AttributeUsage(AttributeTargets.Class) class
MyJscriptClassAttribute extends MyNamespace.MyCsharpClassAttribute {
public function MyJscriptClassAttribute(name) {
}
}

public MyJscriptClassAttribute("Hello, world.")
class MyOtherJsClass extends MyNamespace.MyCsClass {
}

Note that I get the same error when I use the MyCsharpClassAttribute
directly in place of MyJscriptClassAttribute. I also get the same
error if MyJscriptClassAttribute extends MyCsharpAttribute instead of
MyCsharpClassAttribute.

Here's the catch: if I change MyJscriptClassAttribute so that it
extends Attribute instead of MyCsharpClassAttribute (i.e. remove C#
attributes from the equation and define the MyJscriptClassAttribute
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.
 
N

Nicholas Paldino [.NET/C# MVP]

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

Zach Mortensen

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.Attribute
rather than my custom attribute. Please see the example JScript code
that I posted earlier.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top