Skip to content

Instantly share code, notes, and snippets.

@nickdarnell
Last active August 29, 2015 14:17
Show Gist options
  • Save nickdarnell/ef92a0aecdbb57bf5e00 to your computer and use it in GitHub Desktop.
Save nickdarnell/ef92a0aecdbb57bf5e00 to your computer and use it in GitHub Desktop.
public static T CastTo<t>(YourBaseRTTIObject self) where T : YourBaseRTTIObject
{
if (self == null)
return null;
// Check if the object is actually the type we're trying to cast to.
if (self.IsKindOf<t>())
{
Type type = typeof(T);
// Check if we've already cached the factory function.
Func<IntPtr, bool, object> factory;
if (!constructorCache.TryGetValue(type, out factory))
{
factory = CreateConstructorDelegate<t>();
constructorCache.Add(type, factory);
}
// Call the factory function and set the casted source to the
// current object.
T castedObject = (T)factory(YourBaseRTTIObject.getCPtr(self).Handle, false);
castedObject.m_castedSource = self;
return castedObject;
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment