How do I assign .Net attributes programmatically? [duplicate] - c#

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Can attributes be added dynamically in C#?
Is it possible to assign .net Attribute to class/method programmatically?
For example:
Can I decorate my custom .net com classes with Guid/ProgId attributes taken from external file? Something like:
typeof(MyComObject).AssignAttribute(new GuidAttribute("..."));
instead of hardcode like:
[Guid("...")]
class MyComObject
{
}
Thank you in advance!

It depends. ICustomTypeDescriptor allows to almost anything you want to almost every part of a class (which might not even exist for that matter), but this particular interface might not be used by whatever system you're trying to feed your object to. PropertyGrid uses this interface extensively, though.

Related

What is Attribute in C# [duplicate]

This question already has answers here:
What are attributes in .NET?
(11 answers)
Closed 6 years ago.
Sometimes I see them with methods, classes etc.
What does it do?
When should i use them?
Example:
[Obsolete]
public static void MyMethod()
{
//some code
}
An attribute is a declarative tag that is used to convey information to runtime about the behaviors of various elements like classes, methods, structures, enumerators, assemblies etc. in your program. You can add declarative information to a program by using an attribute
https://msdn.microsoft.com/en-us/library/aa288454(v=vs.71).aspx

JSON field's names in the Web Service response [duplicate]

This question already has answers here:
Serializing F# Record type to JSON includes '#' character after each property
(2 answers)
Closed 7 years ago.
I use F#, but I believe the question is not F# specific.
I have the interface for Web Service:
[<ServiceContract>]
type IRestService =
[<OperationContract>]
[<WebGet(UriTemplate = "Maintenance", ResponseFormat=WebMessageFormat.Json)>]
abstract GetMaintenancesRest: a:unit -> Maintenance[]
When I am trying to use this service I can get JSON, but all field's names in the JSON have the symbol '#':
[{"Address#":"one","Assetid#":"","Assignmentdate#":"/Date(1434147917730-0700)/","Comment#":"" ...
Why and how can I fix it?
It's F#. I ran into this a long time ago, so I might be forgetting. As I recall you may not use records to return from the service, or you get this error. You need class types with properties. I don't think mutable records work either. I also don't think public fields work; it's got to be public read/write properties.

I just learned you can have nested classes in C#, what is the point of this? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What are reasons why one would want to use nested classes?
I found some example code online for something unrelated, and it had nested classes. I thought this was a mistake, and the outer one should have been Namespace, but the code compiled and worked fine.
What are nested classes used for? Is this a good programming practice?
So that you don't litter a namespace with classes. Inner, nested classes are visible only to the parent class, or when typing ParentClass.*, rather than spamming the namespace with classes that are only ever used once by one single class
It allows you to logically group all elements of a class within a single file (including subclasses)
More maintainable. It's easier to read small classes in its parent class rather than having to open another file to read a few lines

implementing a custom cast for two types in c# [duplicate]

This question already has answers here:
C# adding implict conversions to existing types
(2 answers)
Closed 9 years ago.
I have two custom classes for which I want to implement casts between each other. I only have the DLLs of the two projects and not the code. Can I use extension methods to implement the cast or would I need to do something else?
I'd suggest that you implement your own mappers between the 2 classes or use mapping tools such as AutoMapper or ValueInjecter
You will have to use either extension methods or some other mapping. You could also use http://automapper.codeplex.com/
I don't think there is a way to do it. Anyway, do you really need the code to look like cast? Sometimes when you implement operators or casts for custom types the code may become harder to understand. I would suggest to create separate utility to convert types which would be more obvious for someone who sees the code for the first time.

How to make a .net(c#) library which can be used in delphi [duplicate]

This question already has answers here:
Closed 13 years ago.
Possible Duplicate:
Create a C# DLL That Can Be Imported in a Delphi App Using stdcall - Possible?
I am creating a c# library and like to make this library as com component which can be accessed from delphi. Please tell the how to achieve this.
How to make .Net (C#) classes accessible through COM is described here. Basicly your classes need to have a default constructor without parameters and you need to decorate your classes with some specific attributes. Then you need to register your assembly using regasm.
Then you can import the ActiveX/COM library in Delphi and call it like it was a regular ActiveX/COM Library.
I won't rewrite, but there is a solution on how to do this here : http://forums.devshed.com/delphi-programming-90/c-dll-need-to-be-used-in-delphi-231122.html
If you aren't opposed to using VB instead of C# there's a cleaner solution. In VB you can use the ComClass item template which does virtually all the work to expose your .NET type to COM for you.

Categories