Compare commits

..

4 Commits

View File

@@ -164,7 +164,7 @@ namespace PepperDash.Essentials.Core
/// </summary>
public abstract class JoinMapBaseAdvanced
{
protected uint _joinOffset;
protected uint JoinOffset;
/// <summary>
/// The collection of joins and associated metadata
@@ -175,36 +175,50 @@ namespace PepperDash.Essentials.Core
{
Joins = new Dictionary<string, JoinDataComplete>();
_joinOffset = joinStart - 1;
JoinOffset = joinStart - 1;
}
protected JoinMapBaseAdvanced(uint joinStart, Type type):this(joinStart)
{
AddJoins(type);
}
protected void AddJoins(Type type)
{
// Add all the JoinDataComplete properties to the Joins Dictionary and pass in the offset
Joins = this.GetType()
.GetCType()
.GetFields(BindingFlags.Public | BindingFlags.Instance)
.Where(field => field.IsDefined(typeof(JoinNameAttribute), false))
.Select(prop => (JoinDataComplete)prop.GetValue(this))
.ToDictionary(join => join.GetNameAttribute(), join =>
{
join.SetJoinOffset(_joinOffset);
return join;
});
//var type = this.GetType();
//var cType = type.GetCType();
//var fields = cType.GetFields(BindingFlags.Public | BindingFlags.Instance);
//foreach (var field in fields)
//{
// if (field.IsDefined(typeof(JoinNameAttribute), true))
// {
// var value = field.GetValue(this) as JoinDataComplete;
// if (value != null)
//Joins = this.GetType()
// .GetCType()
// .GetFields(BindingFlags.Public | BindingFlags.Instance)
// .Where(field => field.IsDefined(typeof(JoinNameAttribute), true))
// .Select(field => (JoinDataComplete)field.GetValue(this))
// .ToDictionary(join => join.GetNameAttribute(), join =>
// {
// value.SetJoinOffset(_joinOffset);
// Joins.Add(value.GetNameAttribute(), value);
// }
// }
//}
// join.SetJoinOffset(_joinOffset);
// return join;
// });
//type = this.GetType(); <- this wasn't working because 'this' was always the base class, never the derived class
var cType = type.GetCType();
var fields = cType.GetFields(BindingFlags.Public | BindingFlags.Instance);
foreach (var field in fields)
{
if (!field.IsDefined(typeof (JoinNameAttribute), true)) continue;
var childClass = Convert.ChangeType(this, type, null);
var value = field.GetValue(childClass) as JoinDataComplete; //this here is JoinMapBaseAdvanced, not the child class...which has no fields.
if (value == null)
{
Debug.Console(0, "Unable to caset base class to {0}", type.Name);
continue;
}
value.SetJoinOffset(JoinOffset);
Joins.Add(value.GetNameAttribute(typeof(JoinDataComplete)), value);
}
PrintJoinMapInfo();
}
@@ -435,26 +449,33 @@ namespace PepperDash.Essentials.Core
_data = customJoinData;
}
public string GetNameAttribute()
public string GetNameAttribute(Type t)
{
string name = string.Empty;
JoinNameAttribute attribute = (JoinNameAttribute)Attribute.GetCustomAttribute(typeof(JoinDataComplete), typeof(JoinNameAttribute));
JoinNameAttribute attribute = (JoinNameAttribute)Attribute.GetCustomAttribute(t, typeof(JoinNameAttribute));
if (attribute != null)
{
name = attribute.Name;
Debug.Console(2, "JoinName Attribute value: {0}", name);
}
return name;
}
}
[AttributeUsage(AttributeTargets.Field)]
[AttributeUsage(AttributeTargets.All)]
public class JoinNameAttribute : Attribute
{
public string Name { get; set; }
private string _Name;
public JoinNameAttribute(string name)
{
Name = name;
Debug.Console(0, "Setting Attribute Name: {0}", name);
_Name = name;
}
public string Name
{
get { return _Name; }
}
}
}