4 Replies - 2598 Views - Last Post: 02 May 2014 - 02:32 PM

#1 optix212  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 27
  • View blog
  • Posts: 509
  • Joined: 10-October 09

making custom attributes.

Posted 02 May 2014 - 06:56 AM

Hey guys,

I'm trying to learn how to implement custom attributes into a class that I have called GameLogger.. which does exactly what the name implies.. It logs events and strings that you pass to it and keeps them nice and organized in a GUI console and XML file for me....

The problem I am having is learning how to use custom attributes... I want my log to be able to have categories, and at the same time, I want to learn how to make attributes, so this is my attempt at that. xP

So, here is my attribute
using UnityEngine;
using System.Collections;
using System;
using System.Reflection;

[AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = true)]
public class CategoryAttribute : Attribute {
	public string Name;

	public CategoryAttribute(string name)
	{
		this.Name = name;
	}

	public string GetName()
	{
		return Name;
	}
}



Soooo, then what I do is make a few classes inside of my GameLog class:
// Your basic log types:
	public static class pe_LogType {

		[Category("Log")]
		public static LogCategory Log;

		[Category("Warning")]
		public static LogCategory Warning;

		[Category("Exception")]
		public static LogCategory Exception;
		
		[Category("ERROR")]
		public static LogCategory Error;

		[Category("Assert")]
		public static LogCategory Assert;

	}
	
	// User adds his own custom log types:
	public static class pe_MoreLogTypes {
		
		[Category("Combat")]
		public static LogCategory Combat;

		[Category("Inventory")]
		public static LogCategory Inventory;

		[Category("Movement")]
		public static LogCategory Movement;
		
	}
	
	public class LogCategory
	{
	}



After that, I set up my AddToLog method, for actually adding the log string with a category:

public static void AddToLog(string eventString, LogCategory level)
	{
		string eventStr = eventString + " @" + System.DateTime.Now.TimeOfDay.Hours + ":" + System.DateTime.Now.TimeOfDay.Minutes + ":" + System.DateTime.Now.TimeOfDay.Seconds.ToString ("F0") + "\n";
		EventLog.Add(GetLogCategory(typeof(LogCategory)) + ": " + eventStr, GetLogCategory(typeof(LogCategory)));  
        if (EventLog.Count >= maxLines)
        {
			EventLog.RemoveAt(0);
		}

    }



I set up a string that will return the Name field of the Attribute, but for some reason I'm getting an error out of it that says "ArguementException: Type is not derived from System.Attribute". Here is the code for my string:

public static string GetLogCategory(Type t)
	{
		MemberInfo info = typeof(LogCategory);
		Attribute att = Attribute.GetCustomAttribute (info, t);
		CategoryAttribute c = (CategoryAttribute)att;
		return c.GetName ();
	}



Any ideas? I have no clue what I'm doing wrong lol.. I'm a complete noob to attributes and reflection.. I literally just started with them today.

P.S: I realize that I could always just use subclasses and generate the category name in the LogCategory constructor... But then I wouldn't learn anything new...

This post has been edited by optix212: 02 May 2014 - 07:00 AM


Is This A Good Question/Topic? 0
  • +

Replies To: making custom attributes.

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3621
  • View blog
  • Posts: 11,278
  • Joined: 05-May 12

Re: making custom attributes.

Posted 02 May 2014 - 09:19 AM

Look closer at the second parameter you are passing to GetCustomAttribute().

Quote

attributeType
Type: System.Type
The type, or a base type, of the custom attribute to search for.

Was This Post Helpful? 0
  • +
  • -

#3 optix212  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 27
  • View blog
  • Posts: 509
  • Joined: 10-October 09

Re: making custom attributes.

Posted 02 May 2014 - 09:24 AM

Right, I ended up getting that settled by using this code instead:

public static string GetLogCategory(Type t)
	{
		MemberInfo info = typeof(LogCategory);
		CategoryAttribute att = (CategoryAttribute)Attribute.GetCustomAttribute (info, typeof(CategoryAttribute));
		CategoryAttribute c = (CategoryAttribute)att;
		return c.GetName ();
	}



But now, instead of that error I'm getting many more.. I don't know how to make it read an attribute from static LogCategory objects that I made... I can pass a LogCategory object through my GetLogCategory method, but then I get lost.. I don't know what to do WITH the LogCategory object that I've passed through it.... I don't know if I should use MemberInfo, FieldInfo, PropertyInfo, etc.. Plus, I don't know how to cast the LogCategory object into any of those.. Then, after that is done, I don't know how to acces the Name field from the actual Attribute.. I think I might be overthinking this, but to me this is all very new.. So please bear with me xP

P.S I can't believe there isn't a tutorial on the forum for this lol. Once I get this working I will type one up for you guys.

This post has been edited by optix212: 02 May 2014 - 09:55 AM

Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3621
  • View blog
  • Posts: 11,278
  • Joined: 05-May 12

Re: making custom attributes.

Posted 02 May 2014 - 10:56 AM

Did you look at the Attributes tutorial in MSDN? http://msdn.microsof...4(v=vs.71).aspx
Was This Post Helpful? 0
  • +
  • -

#5 optix212  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 27
  • View blog
  • Posts: 509
  • Joined: 10-October 09

Re: making custom attributes.

Posted 02 May 2014 - 02:32 PM

Hey skydiver, thanks for the help. But after further review and careful consideration, I've come to the conclusion that I'm an idiot. After all of it, I realized that subclasses are the way to go. I set up my LogCategory class with a default constructor that fills a Name field, and then made a class that holds a bunch of LogCategory objects. Works like a charm. In other words, this is NOT the way Attributes are designed to work. It is possible, but would have been insanely slower than the subclass approach. Attributes are type-specific.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1