Skip to content

Instantly share code, notes, and snippets.

@rid00z
Created March 22, 2016 23:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rid00z/be387b548fd5cdd551d0 to your computer and use it in GitHub Desktop.
Save rid00z/be387b548fd5cdd551d0 to your computer and use it in GitHub Desktop.
Text Blob Operation and Attribute
public static class TextBlobOperations
{
public static void GetTextBlobs(object element)
{
if (element == null)
return;
var type = element.GetType();
foreach (var relationshipProperty in type.GetTextBlobProperties())
{
var relationshipAttribute = relationshipProperty.GetAttribute<TextBlobAttribute>();
if (relationshipAttribute is TextBlobAttribute)
{
TextBlobOperations.GetTextBlobChild(element, relationshipProperty);
}
}
}
private static void GetTextBlobChild(object element, PropertyInfo relationshipProperty)
{
if (element == null)
return;
var type = element.GetType();
var relationshipType = relationshipProperty.PropertyType;
Debug.Assert(relationshipType != typeof(string), "TextBlob property is already a string");
var textblobAttribute = relationshipProperty.GetAttribute<TextBlobAttribute>();
var textProperty = type.GetRuntimeProperty(textblobAttribute.TextProperty);
Debug.Assert(textProperty != null && textProperty.PropertyType == typeof(string), "Text property for TextBlob relationship not found");
var textValue = (string)textProperty.GetValue(element, null);
var value = textValue != null ? JsonConvert.DeserializeObject (textValue, relationshipType) : null;
relationshipProperty.SetValue(element, value, null);
}
public static void UpdateTextBlobs(object element)
{
if (element == null)
return;
var type = element.GetType();
foreach (var relationshipProperty in type.GetTextBlobProperties())
{
var relationshipAttribute = relationshipProperty.GetAttribute<TextBlobAttribute>();
if (relationshipAttribute is TextBlobAttribute)
{
TextBlobOperations.UpdateTextBlobProperty(element, relationshipProperty);
}
}
}
private static void UpdateTextBlobProperty(object element, PropertyInfo relationshipProperty)
{
if (element == null)
return;
var type = element.GetType();
var relationshipType = relationshipProperty.PropertyType;
Debug.Assert(relationshipType != typeof(string), "TextBlob property is already a string");
var textblobAttribute = relationshipProperty.GetAttribute<TextBlobAttribute>();
var textProperty = type.GetRuntimeProperty(textblobAttribute.TextProperty);
Debug.Assert(textProperty != null && textProperty.PropertyType == typeof(string), "Text property for TextBlob relationship not found");
var value = relationshipProperty.GetValue(element, null);
var textValue = value != null ? JsonConvert.SerializeObject(value) : null;
textProperty.SetValue(element, textValue, null);
}
}
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class TextBlobAttribute : IgnoreAttribute
{
public TextBlobAttribute(string textProperty)
{
TextProperty = textProperty;
}
public string TextProperty { get; private set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment