Generic way to get properties on Component?

I need to dump selected GameObjects to a file, with all their components and their components variable names and values.

Is there a generic 'getAttribute' kind of API I can call if I have a Component and I don't know the type?

You could always abuse reflection and generics for it.

Something like:

using System.Reflection;

//blah blah

public static T GetReference<T>(object inObj, string fieldName) where T : class
{
    return GetField(inObj, fieldName) as T;
}

public static T GetValue<T>(object inObj, string fieldName) where T : struct
{
    return (T)GetField(inObj, fieldName);
}

public static void SetField(object inObj, string fieldName, object newValue)
{
    FieldInfo info = inObj.GetType().GetField(fieldName);
    if (info != null)
        info.SetValue(inObj, newValue);
}

private static object GetField(object inObj, string fieldName)
{
    object ret = null;
    FieldInfo info = inObj.GetType().GetField(fieldName);
    if (info != null)
        ret = info.GetValue(inObj);
    return ret;
}

This has the benefit of working on any object, not just components. Use GetValue when you know it'll be a value type, GetReference when it'll be a reference type. Added SetField just for fun too (which works without generics for obvious reasons)

Hopefully pretty self explanatory

This should get me started:

using UnityEngine;
using UnityEditor;
using System;
using System.Reflection;

class Introspect
{

    [MenuItem("Window/Introspect")]
    static void IntrospectNow()
    {
        bool found1;
        GameObject[] go = Selection.gameObjects;
        Transform[] trs = Selection.GetTransforms (SelectionMode.Deep | SelectionMode.DeepAssets);
        found1 = false;
        foreach (Transform tr in trs)
        {
            Component[] components = tr.GetComponents<Component>();
            for (int i = 0; i < components.Length; i++)
            {
                Component c = components*;*
 *if (c == null)*
 *{*
 *Debug.Log(tr.name + " has an empty script attached in position: " + i);*
 *found1 = true;*
 *}*
 *else*
 *{*
 *Type t = c.GetType();*
 *Debug.Log("Type "+t);*
 *Debug.Log("Type information for:" + t.FullName);*
 *Debug.Log("	Base class = " + t.BaseType.FullName);*
 *Debug.Log("	Is Class = " + t.IsClass);*
 *Debug.Log("	Is Enum = " + t.IsEnum);*
 *Debug.Log("	Attributes = " + t.Attributes);* 
 *System.Reflection.FieldInfo[] fieldInfo = t.GetFields();*
 *foreach (System.Reflection.FieldInfo info in fieldInfo)*
 *Debug.Log("Field:" +info.Name);*
 *System.Reflection.PropertyInfo[] propertyInfo = t.GetProperties();*
 *foreach (System.Reflection.PropertyInfo info in propertyInfo)*
 *Debug.Log("Prop:"+info.Name);*
 *Debug.Log("Found component "+c);*
 *}*
 *}*
 *}*
 *}*
*}*
*```*

SerializeHelper can do what you ask for, at least for Transform and MonoBehavior components; Other types can be supported with some minor additions.