scriptsharp icon indicating copy to clipboard operation
scriptsharp copied to clipboard

Custom attributes

Open Ventajou opened this issue 13 years ago • 8 comments

Hi,

I'm trying to create some custom attributes to use on some classes. I figured out that they have to be declared in a plain c# class library (not a Script# one) and the project builds well. But when I try to reference it in a Script# project, the ScriptCompiler task throws the following error:

Unable to resolve referenced type 'System.Attribute'. Make sure all needed assemblies have been explicitly referenced.

Is there anything I can do? Or anything you could fix? Thanks!

Ventajou avatar Feb 24 '12 05:02 Ventajou

Did you compile your attribute in a c# project that references the script# mscorlib assembly? In other words, in the script# world, you cannot reference the regular .net mscorlib.dll.

Best would be to start with the ImportLibrary project template. That is set up to compile with the right mscorlib, but doesn't invoke the script# compiler step, i.e. it only produces an assembly and doesn't generate any script.

nikhilk avatar Feb 24 '12 16:02 nikhilk

The only reference is the Script# mscorlib.dll, I never even noticed the ImportLibrary template so I'll give that one a shot in case there is something I missed.

Ventajou avatar Feb 24 '12 18:02 Ventajou

Ok, I can reproduce the issue using the Script# 0.7.4 project templates. I can send you a zip of the solution but it should only take minutes for you to reproduce:

  • Create a new solution with a Script Library.
  • Add a new Import Library to the solution.
  • Reference your Import project in your Script project.
  • Add a class like this to your Import project:
[AttributeUsage(AttributeTargets.Class, Inherited = true, AllowMultiple = false)]
[Imported]
[NonScriptable]
public class MyAttribute : Attribute
{
}

When you compile, you'll get the error I reported above.

Ventajou avatar Feb 25 '12 02:02 Ventajou

I encountered the same problem , but was able to circumvent it by creating an Import library that contains a single file containing:

namespace System { public class Attribute { } }

If you reference it from the library that's causing your problems it fixes the issue (a warning is generated, though, but at least the build succeeds)

alonweiss avatar Mar 11 '12 07:03 alonweiss

I encountered the same problem. I want the code to be use in C# ,also Script#, I can't circumvent it even if I Create an Import library. Please Help me.Hurry!

whistwind avatar Apr 09 '12 06:04 whistwind

You need 3 projects:

  1. Import library Project named "AttributeImportLibrary", with one file that contains the code i wrote above: using System;

namespace System { public class Attribute { } }

  1. ImportLibrary1 with your attribute class: namespace MyNamespace { [AttributeUsage(AttributeTargets.Class)] public class MyAttribute : Attribute { } }
  2. Your actual Script# project that references the two other projects:

namespace ScriptLibrary1 { [My] public class Class1 { } }

Works for me.

And here's another tip - use SharpKit. It's 1000x time better.

Alon.

alonweiss avatar Apr 09 '12 10:04 alonweiss

Thanks,the method can work!

whistwind avatar Apr 10 '12 03:04 whistwind

Tried this, but got /'MyAttribute' is not an attribute class/ from Script# at the usage point

lostmsu avatar Feb 04 '13 21:02 lostmsu