Last active
August 29, 2015 14:17
-
-
Save nickdarnell/ef92a0aecdbb57bf5e00 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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