This question already has answers here:
Find a private field with Reflection?
(11 answers)
Attributes on Field in Class
(2 answers)
Closed 2 years ago.
I am not sure yet what is the best way to do this, I found that you can assign attributes to a class but there is no way to assign an attribute to a variable of the class.
Example
[attribute author....]
public class ABC
int variable1;
what I am looking for is to assign an attribute to variable1.
so something like this
[attribute author....]
public class ABC
[attribute or something to tie to variable1]
int variable1;
So when I use reflection to access the class, I can get additional information from each variable. The reason for this is that I am creating an editor and I want to group variables in the editor but I don't want to hard code the variable names or keep a list of variables that should be grouped.
Any idea how to make something like that?
class_name.GetType().GetFields() returns the class variables aka fields.
Use a Dictionary to store attributes for these fields.
Related
This question already has answers here:
When should I use a struct rather than a class in C#?
(31 answers)
Closed 4 years ago.
Why POCO objects are created using classes instead of structs in C#, while POCO is intended to carry only public attributes and not any behaviors?
Do we really need to create a class with get and set accessors? I think structs would be much cleaner and simple to carry object's state.
In C#, struct type is a value, so looks that it is not a good choice.
This question already has answers here:
What is the use of static variable in C#? When to use it? Why can't I declare the static variable inside method?
(12 answers)
Closed 7 years ago.
Does anybody know how to modify a static variable from a different script in Unity?
Please, provide more information about how are you trying to do this...
If you need to change between mono behaviours, for example, you will need to get current instance of behaviour you like to change in a way like
myGameObjectInstance.GetComponent<MyBehaviourWithStatic>().myStatic = value;
but note that user2320445 answer is not wrong, it depends on your context. Be more specific on your question and we could provide better and precise answers
This question already has answers here:
Can you add to an enum type in run-time
(7 answers)
Closed 8 years ago.
I have this enum function with some elements:
public enum TrackingTypeEnum
{
None,
Start,
PageView,
Foreground,
Background,
Push,
}
And I want to add some elements there, but not manually in the function, i want to use "new" command but dosn't work.
I've tried this:
TrackingTypeEnum Custom = new TrackingTypeEnum;
Any solution?
Thanks!
According to official documentation, you can't use enum keywork in such a way. In sort, an enum is a list of constants you can assign values to.
Read: http://msdn.microsoft.com/en-us/library/sbbt4032.aspx
That's not what enums are for- you should only use them to store every possible state of an object.
If you really do want to use enums, you can store those additional values in a dictionary, like it's covered in this SO answer:
C#: can you add to an enum type in run-time
This question already has answers here:
Pass An Instantiated System.Type as a Type Parameter for a Generic Class
(6 answers)
Closed 9 years ago.
I have a class definition like this:
public class LuRequest<T>
{
...
}
I want to create a instance of it by assigning the type T dynamically like
Type t = OtherObject.GetType();
LuRequest< t> inst = new LuRequest<t>();
However it does not allow me do so, everything in the brackets need to be in compile time.
Is there any way I can do that?
Thank you
Try to use Activator.CreateInstance() method.
http://msdn.microsoft.com/en-gb/library/system.activator.createinstance.aspx
This question already has answers here:
When do you use the "this" keyword? [closed]
(31 answers)
In C#, is "this" keyword required? [duplicate]
(6 answers)
Closed 9 years ago.
I would like to know if I get it correctly: also with this keyword I can distinct between fields and variables?
Like this:
class X
{
int x;
public X(int x)
{
this.x=x;
}
}
Yes, if a method parameter (or local variable) has the same name as a field, you need to use this to distinguish the two. Also, StyleCop is very vocal about every class member access being done through this, but whether that's a good idea or not may be up to debate. It makes things more clear, but also adds much visual clutter.