Skip to content

Instantly share code, notes, and snippets.

@abc126
Created October 8, 2018 01:44
Show Gist options
  • Select an option

  • Save abc126/0f7e8b049fd61954a971acd09fa64d8c to your computer and use it in GitHub Desktop.

Select an option

Save abc126/0f7e8b049fd61954a971acd09fa64d8c to your computer and use it in GitHub Desktop.
PresenterBindingAttribute
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MVP
{
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public sealed class PresenterBindingAttribute : Attribute
{
public Type PresenterType { get; private set; }
public PresenterBindingAttribute(Type presenterType)
{
PresenterType = presenterType;
}
}
public static class ViewExtension
{
public static object BindingPresenter(this ViewWithPresenterBase viewInstance)
{
object presenter = null;
Type t = viewInstance.GetType();
object[] attrs = t.GetCustomAttributes(typeof(PresenterBindingAttribute), false);
foreach (PresenterBindingAttribute pba in attrs)
{
Type newt = pba.PresenterType;
presenter = Activator.CreateInstance(pba.PresenterType, viewInstance);
}
return presenter;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment