Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds extension method for twin element (from ixf) #113

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions src/ix.connectors/src/Ix.Connector/TwinPrimitiveExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
// Third party licenses: https://github.com/ix-ax/ix/blob/master/notices.md

using System;
using System.Linq;
using System.Reflection;
using Ix.Connector.ValueTypes;

namespace Ix.Connector;
Expand Down Expand Up @@ -82,4 +84,64 @@ public static T GetShadowValue<T>(this OnlinerBase primitive)
if (primitive == null) throw new ArgumentNullException("Value cannot be null");
return ((dynamic)primitive).Shadow;
}

/// <summary>
/// Gets property information using symbol information to access the property information from particular instance.
/// </summary>
/// <param name="twinElement">Twin element about which the property information is to be retrieved.</param>
/// <returns>Property information declared on a particular instance of a twin element.</returns>
public static PropertyInfo GetPropertyInfoViaSymbol(this ITwinElement twinElement)
{
if (twinElement == null) return null;
var propertyName = string.Join("", twinElement.GetSymbolTail().TakeWhile(p => !p.Equals('.')));

if (twinElement.Symbol == null)
return null;

if (twinElement.Symbol.EndsWith("]"))
{
propertyName = propertyName?.Substring(0, propertyName.IndexOf('[') - 1);
}

var propertyInfo = twinElement?.GetParent()?.GetType().GetProperty(propertyName);

return propertyInfo;
}

/// <summary>
/// Get attribute of a particular type defined in the declaration of a twin element.
/// </summary>
/// <typeparam name="T">Attribute type</typeparam>
/// <param name="twinElement">Element for which the information is to be retrieved.</param>
/// <returns>Attribute</returns>
public static T GetAttribute<T>(this ITwinElement twinElement) where T : Attribute
{
if (twinElement == null) return null;

try
{
var propertyInfo = GetPropertyInfoViaSymbol(twinElement);
if (propertyInfo != null)
{
if (propertyInfo.GetCustomAttributes().FirstOrDefault(p => p is T) is T propertyAttribute)
{
return propertyAttribute;
}
}

if (twinElement
.GetType()
.GetCustomAttributes(true)
.FirstOrDefault(p => p is T) is T typeAttribute)
{
return typeAttribute;
}
}
catch (Exception)
{
// Swallow;
}

return null;
}
}