vendredi 27 mars 2015

How do I find out if a Property / Field is a Reference Type (using Reflection)

I have already checked out this link. There was the problem, that generally there was an unexact terminology of "reference". So firstly I want to clarify when in my opinion a Type is a reference Type.


This is the behavior of a not reference Type.



string bla = "abc";
string blob = bla;
bla = "cde";
// bla = "cde" and blob = "abc"


This is the behavior of a reference Type



Person jens = new Person("Jens");
Person frank = jens; // doesn't make any sense i know
frank.Name = "Frank";
//jens.Name = "Frank" and frank.Name = "Frank"


As you can see once you reference only Types pass a Reference (like a Pointer in c). You can also achieve that behavior for nonreference Types by using the ref Keyword.


What I am searching for is a Property of the Type Class that indicates whether the type is a reference type or not.


It should also work for structs, simply for everything where you can "store" something in it.


According to the link I provided above there are multiple Properties, all slightly different



  • IsClass

  • IsPrimitive

  • IsValueType


Which one do I need do use? None of them seems to fit my conditions.


sorry for my bad english :)






Aucun commentaire:

Enregistrer un commentaire