# Add \[Serializable\] attribute to C# output code

**URL:** <https://community.haxe.org/t/add-serializable-attribute-to-c-output-code/2448>\
**Category:** Haxe\
**Tags:** haxe-cs\
**Created:** [May 18, 2020, 4:27am UTC](https://community.haxe.org/t/add-serializable-attribute-to-c-output-code/2448 "2020-05-18T04:27:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kyliathy](https://community.haxe.org/user_avatar/community.haxe.org/kyliathy/32/452_2.png) [@Kyliathy](https://community.haxe.org/u/Kyliathy)\
**Post date:** [May 18, 2020, 4:27am UTC](https://community.haxe.org/t/add-serializable-attribute-to-c-output-code/2448/1 "2020-05-18T04:27:48Z")

</div>

I want to use the XmlSerializer or DataContract serializer in C#. These rely on reflection and attributes with which their targets are marked. How can I add custom attributes in C#, such as [Serializable] or, even more importantly, attributes that have arguments, such as:

`[DataContract(Name = "SomeArgument", Namespace = "http://www.somewebsite.io")]`

---

<div class="post-metadata">

**Author:** ![dmitryhryppa](https://community.haxe.org/user_avatar/community.haxe.org/dmitryhryppa/32/1815_2.png) [@dmitryhryppa](https://community.haxe.org/u/dmitryhryppa)\
**Post date:** [May 18, 2020, 6:19pm UTC](https://community.haxe.org/t/add-serializable-attribute-to-c-output-code/2448/2 "2020-05-18T18:19:45Z")

</div>

Hi!  
There is `@:strict` which is used to declare a native C# attribute.

Example:

```haxe
import cs.system.AttributeTargets;
import cs.system.AttributeUsageAttribute;
import cs.system.Attribute;

final class Main {
	@:strict(DataContract({Name:"SomeArgument", Namespace: "http://www.somewebsite.io"}))
	static function main() {
	}
}

@:nativeGen
@:strict(AttributeUsageAttribute(AttributeTargets.Method))
class DataContract extends Attribute {
	@:property
	public var Name:String;

	@:property
	public var Namespace:String;

	public function new() {
		super();
	}
}

```

Which will generate the following c# code:

```cs
	[DataContract(Name = "SomeArgument", Namespace = "http://www.somewebsite.io")]
	public static void main() {
	}

```

I hope it helps.

---

<div class="post-metadata">

**Author:** ![RoBBoR](https://community.haxe.org/user_avatar/community.haxe.org/robbor/32/1881_2.png) [@RoBBoR](https://community.haxe.org/u/RoBBoR)\
**Post date:** [April 22, 2022, 1:46pm UTC](https://community.haxe.org/t/add-serializable-attribute-to-c-output-code/2448/3 "2022-04-22T13:46:56Z")

</div>

Sorry for bump on super old topic, but this thread is the only resource on adding native attributes in C#, so I just wanted to add if you’re having trouble using `@:strict` because of the type-checker, you can also just use `@:meta`, which will add the provided string directly to an attribute in C#.

Literally just discovered this metadata, and it’s saved my life. I was writing some classes in separate C# files just because it was impossible to make the Haxe compiler accept that my attributes existed.

Example:

```haxe
@:nativeGen
@:meta(DataContract(Name="SomeArgument", Namespace="http://www.somewebsite.io"))
class MyClass {
	function main() {
	}
}

```

Will generate:

```cs
[DataContract(Name="SomeArgument", Namespace="http://www.somewebsite.io")]
class MyClass {
	void main() {
	}
}

```
