How to Get a Type's Alias Through Reflection

Is there a way to get a type's alias through reflection?

Nope - just create a Dictionary<Type,string> to map all of the types to their aliases. It's a fixed set, so it's not hard to do:

private static readonly Dictionary<Type, string> Aliases =
new Dictionary<Type, string>()
{
{ typeof(byte), "byte" },
{ typeof(sbyte), "sbyte" },
{ typeof(short), "short" },
{ typeof(ushort), "ushort" },
{ typeof(int), "int" },
{ typeof(uint), "uint" },
{ typeof(long), "long" },
{ typeof(ulong), "ulong" },
{ typeof(float), "float" },
{ typeof(double), "double" },
{ typeof(decimal), "decimal" },
{ typeof(object), "object" },
{ typeof(bool), "bool" },
{ typeof(char), "char" },
{ typeof(string), "string" },
{ typeof(void), "void" }
};

Retrieve type alias alternate name using reflection

Per the proposal, type aliases are mostly invisible at runtime. The exception is in the name of embedded fields.

With the types in the question, there's no way to distinguish the alias from the original type.

Programmatically get a Type's Alias in .NET

Not directly with reflection, there is not. The "type name" (as you are calling it) "int" is not a type name at all, but a language keyword.

You could, of course, use a dictionary to store the look ups from type names to the shorter convenience forms. But there is nothing in the reflection API's that will do that for you.

How to get the aliased type of a type alias in scala runtime?

You can dealias the type from the tag:

scala> type INeedDetails = (Int, String, Unit, Nothing, Float)
defined type alias INeedDetails

scala> typeTag[INeedDetails].tpe
res1: reflect.runtime.universe.Type = INeedDetails

scala> typeTag[INeedDetails].tpe.dealias
res2: reflect.runtime.universe.Type = (scala.Int, String, scala.Unit, scala.Nothing, scala.Float)

Reflection on type aliases

I think normalize method on Type may help you:

Expands type aliases and converts higher-kinded TypeRefs to PolyTypes.
Functions on types are also implemented as PolyTypes. Example: (in the
below, is the type constructor of List) TypeRef(pre, , List()) is
replaced by PolyType(X, TypeRef(pre, , List(X)))

Having a type t you can also do something like t.map(_.normalize). This will normalize t as well as all types that are part of definition of t (e.g. type arguments). So effectively this will get rid of any aliases that occur anywhere in t.

How to get alias of a type?

Well, I don't believe there is no way to do it programmaticly. You can use a dictionary instead of like;

public static readonly Dictionary<Type, string> aliases = new Dictionary<Type, string>()
{
{ typeof(string), "string" },
{ typeof(int), "int" },
{ typeof(byte), "byte" },
{ typeof(sbyte), "sbyte" },
{ typeof(short), "short" },
{ typeof(ushort), "ushort" },
{ typeof(long), "long" },
{ typeof(uint), "uint" },
{ typeof(ulong), "ulong" },
{ typeof(float), "float" },
{ typeof(double), "double" },
{ typeof(decimal), "decimal" },
{ typeof(object), "object" },
{ typeof(bool), "bool" },
{ typeof(char), "char" }
};

EDIT: I found two questions to provide an answer

  • Is there a way to get a type's alias through reflection?
  • Possible to get type of an aliased type in c#?

Assigning to type definition using reflection in Go

I ended up using the Convert function in conjunction with the Assignable function to do my check:


// Get value to assign and its type
vValue := reflect.ValueOf(getInteger())
tValue := vValue.Type()

// Check if the field can be assigned the value; if it can then do so
// Otherwise, check if a conversion can be assigned
vType := vField.Type()
if tValue.AssignableTo(vType) {
vField.Set(vValue)
} else if vValue.CanConvert(vType) {
vField.Set(vValue.Convert(vType))
}

This fixed my issue.

How to determine a type alias with Scala Reflection?

This is a bug: https://issues.scala-lang.org/browse/SI-6474 caused by the fact that Type.typeSymbol automatically dereferences aliases.

scala> showRaw(typeOf[String])
res0: String = TypeRef(SingleType(ThisType(scala), scala.Predef), newTypeName("String"), List())

scala> typeOf[String].typeSymbol
res1: reflect.runtime.universe.Symbol = class String

scala> typeOf[String].typeSymbol.asType.isAliasType
res2: Boolean = false

scala> val TypeRef(_, sym, _) = typeOf[String]
sym: reflect.runtime.universe.Symbol = type String

scala> sym.asType.isAliasType
res3: Boolean = true

A workaround, as partially provided by the REPL printout, is to perform manual pattern matching and extract the underlying symbol. An alternative is to cast to scala.reflect.internal.Types#Type and use typeSymbolDirect.



Related Topics



Leave a reply



Submit