Finding the type of a given variable

Given a variable v, how do i determine if v is

  1. an object? or
  2. a component? or
  3. a property? or
  4. a field?

Additionally, can we find if v (if its a property/field) is:

  1. a value ? or
  2. a reference ?

If possible, one line codes would be preferable.

Thanks,
Amith

(The following answer assumes Unity Javascript. I’m not as familiar with the finer details of C# so I can’t quite speak to that)

Well:

  1. All vars, to my knowledge, are objects.
  2. Use the following:
var variableAsComponent : Component = testVar;

if variableAsComponent is not null, then testVar is an instance of a class derived from Component

3 and 4)
You’ll have to use System.Reflection, and have the name of the variable and the class of which it might be a property or a field. (A reference is insufficient in this case because it lacks context)

import System.Reflection;

var theType : Type = testMonoBehaviour.GetType();
var thePropertyInfo : PropertyInfo = theType.GetProperty("potentialPropertyName");

Again, if thePropertyInfo is non-null, then it’s a property of that class. You can do the same thing with Method, Member, and Field by changing every instance of “Property” in that example.

Your next question is not really relevant. All objects - aside from primitives (and, I think - but am not 100% sure - structs) - are always passed by reference.

Type is a class of the System namespace. There’s no need for reflection for that, if you only want to know the type.

var v3 :Vector3;
if (v3.GetType() == Vector3) {
    v3.Normalize();
}

You can check if it’s a value or reference type with the IsValueType() method.

v3.GetType().IsValueType();

However you can also just look at the Unity Reference. Anything marked as Struct is a value type while anything marked as Class is a reference type.