469,592 Members | 1,674 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,592 developers. It's quick & easy.

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(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.
Nov 15 '05 #1
2 2158
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]
- mv*@spam.guard.caspershouse.com

"Zach Mortensen" <zm********@hotmail.com> wrote in message
news:c9**************************@posting.google.c om...
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.

Nov 15 '05 #2
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.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:<uX**************@tk2msftngp13.phx.gbl>...
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.

Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Aaron Prohaska | last post: by
1 post views Thread by Joe Bloggs | last post: by
5 posts views Thread by | last post: by
3 posts views Thread by Microsoft Newsgroups | last post: by
4 posts views Thread by zubair | last post: by
5 posts views Thread by IkBenHet | last post: by
13 posts views Thread by Lee Newson | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.