pass value through custom attribute for method access - c#

Can any one please tell me about custom attribute for method.I need to pass a string to attribute.If the string is true then i will access the method otherwise don't access the method.

I am not sure, if I understood your question wrongly. But are you talking about following kind of attribute which decorates the method. I had created this code when I was exploring attributes. I am pasting it here. Hope it helps.
In this, I have created the attribute, [Allow("Valid")] if it is valid we can call the method , else not.
namespace ConsoleApplication1
{
using System;
[AttributeUsage(AttributeTargets.All)]
public class AllowAttribute : System.Attribute
{
public readonly string SomeString;
public AllowAttribute(string someString) // your string is passed in custom attribute
{
this.SomeString = someString;
}
}
public interface IAllowAttributeInvoker
{
object AllowAttributeInvokeMethod<T>(string methodName, T classInstance, object[] parametersArray);
}
public class AllowAttributeInvoker: IAllowAttributeInvoker
{
public object AllowAttributeInvokeMethod<T>(string methodName, T classInstance, object[] parametersArray)
{
System.Reflection.MemberInfo info = typeof(T).GetMethod(methodName);
if (IsAttributeValid(info))
{
var method = (typeof (T)).GetMethod(methodName);
Console.WriteLine("Invoking method");
var result = method.Invoke(classInstance, parametersArray);
return result;
}
else
{
Console.WriteLine("Can not invoke this method.");
}
return null;
}
private static bool IsAttributeValid(MemberInfo member)
{
foreach (object attribute in member.GetCustomAttributes(true))
{
if (attribute is AllowAttribute && ((AllowAttribute)attribute).SomeString == "Valid")
{
return true;
}
}
return false;
}
}
public class EmployeeService :AllowAttributeInvoker
{
public object PaySalary()
{
return AllowAttributeInvokeMethod("PaySalaryInvoke", this, null);
}
[Allow("Valid")]
public void PaySalaryInvoke()
{
Console.WriteLine("Salary Paid.");
}
}
class Program
{
static void Main(string[] args)
{
Console.ReadLine();
EmployeeService service = new EmployeeService();
service.PaySalary();
Console.ReadLine();
}
}
}

1-You can define public user access list:
public List<string> AccessRules = new List<string>();
2-Set user access rules in constructor :
AccessRules.AddRange(new[] { "GetCurrentDateTime", "GetCurrentDate" });
3-In secure methods check user access rule
public DateTime GetCurrentDateTime()
{
bool haveAccess = AccessRules.Any(c => c == "GetCurrentDateTime");
if (haveAccess)
{
return DateTime.Now;
}
return null;
}

Related

Is there a way for me to access a C# class attribute?

Is there a way for me to access a C# class attribute?
For instance, if I have the following class:
...
[TableName("my_table_name")]
public class MyClass
{
...
}
Can I do something like:
MyClass.Attribute.TableName => my_table_name
Thanks!
You can use Attribute.GetCustomAttribute method for that:
var tableNameAttribute = (TableNameAttribute)Attribute.GetCustomAttribute(
typeof(MyClass), typeof(TableNameAttribute), true);
However this is too verbose for my taste, and you can really make your life much easier by the following little extension method:
public static class AttributeUtils
{
public static TAttribute GetAttribute<TAttribute>(this Type type, bool inherit = true) where TAttribute : Attribute
{
return (TAttribute)Attribute.GetCustomAttribute(type, typeof(TAttribute), inherit);
}
}
so you can use simply
var tableNameAttribute = typeof(MyClass).GetAttribute<TableNameAttribute>();
You can use reflection to get it. Here's is a complete encompassing example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
public class TableNameAttribute : Attribute
{
public TableNameAttribute(string tableName)
{
this.TableName = tableName;
}
public string TableName { get; set; }
}
[TableName("my_table_name")]
public class SomePoco
{
public string FirstName { get; set; }
}
class Program
{
static void Main(string[] args)
{
var classInstance = new SomePoco() { FirstName = "Bob" };
var tableNameAttribute = classInstance.GetType().GetCustomAttributes(true).Where(a => a.GetType() == typeof(TableNameAttribute)).Select(a =>
{
return a as TableNameAttribute;
}).FirstOrDefault();
Console.WriteLine(tableNameAttribute != null ? tableNameAttribute.TableName : "null");
Console.ReadKey(true);
}
}
}
Here's an extension that will make it easier, by extending object to give you an attribute helper.
namespace System
{
public static class ReflectionExtensions
{
public static T GetAttribute<T>(this object classInstance) where T : class
{
return ReflectionExtensions.GetAttribute<T>(classInstance, true);
}
public static T GetAttribute<T>(this object classInstance, bool includeInheritedAttributes) where T : class
{
if (classInstance == null)
return null;
Type t = classInstance.GetType();
object attr = t.GetCustomAttributes(includeInheritedAttributes).Where(a => a.GetType() == typeof(T)).FirstOrDefault();
return attr as T;
}
}
}
This would turn my previous answer into:
class Program
{
static void Main(string[] args)
{
var classInstance = new SomePoco() { FirstName = "Bob" };
var tableNameAttribute = classInstance.GetAttribute<TableNameAttribute>();
Console.WriteLine(tableNameAttribute != null ? tableNameAttribute.TableName : "null");
Console.ReadKey(true);
}
}

How to get a default generic delegate for a type

I have a generic delegate like this:
public delegate T SomeHandler<T>(T input);
I have a generic class that take the delegate as a parameter to its constructor like this:
public class SomeClass<T>
{
private SomeHandler<T> m_handler;
public SomeClass(SomeHandler<T> handler)
{
m_handler = handler;
}
public void DoSomeStuff(T input)
{
T result = m_handler(input);
// some stuff
}
}
Most of the time I would instantiate the class with a default handler unless some special case is needed. So I have some default handlers for the types I use:
public static class DefaultHandlers
{
public static string DefaultStringHandler(string input)
{
return input;
}
}
In some cases, the type is instantiated with a special handler that is specific to its implementation:
public class Example
{
private SomeClass<string> m_typicalCase;
private SomeClass<string> m_specialCase;
public Example()
{
m_typicalCase = new SomeClass<string>(DefaultHandlers.DefaultStringHandler);
m_specialCase = new SomeClass<string>(SpecialHandler);
}
private string SpecialHandler(string input)
{
string result;
// Do something special
return result;
}
}
I want to create a default constructor for SomeClass that always instantiates the class with the same default handler for that type, but since the type is not know at compile time, I can't return the delegate that is the right type.
public class SomeClass<T>
{
...
public SomeClass()
{
m_handler = DefaultHandlers.GetDefaultHandler<T>();
}
...
}
Like this
public static class DefaultHandlers
{
public static SomeHandler<T> GetDefaultHandler<T>()
{
if (typeof(T) == typeof(string))
{
return DefaultStringHandler;
}
}
}
This does not work becuase DefaultStringHandler returns a string and the method expects T.
The only way that I have found to do this is the make a type-specific subclass of SomeClass that overloads the default constructor:
public class SomeStringClass : SomeClass<string>
{
public SomeStringClass()
: base(DefaultHandlers.DefaultStringHandler)
{
}
public SomeStringClass(SomeHandler<string> handler)
: base(handler)
{
}
}
It would be fun if generic types could have type-specific overloaded constructors that are used when instantiating the class of a specific type:
public class Foo<T>
{
public Foo<string>(string input)
{
}
public Foo<int>(int input)
{
}
public Foo(T input)
{
}
}
There must be a more elegant way to do with with a design pattern, Strategy maybe?
You could utilize dynamic to get something like SomeClass<string>():
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Zoltan
{
public class SomeClass<T>
{
private static readonly Func<T,T> FALL_BACK_HANDLER = a => a; //or what have you
private readonly Func<T,T> m_handler;
public SomeClass(Func<T,T> handler)
{
m_handler = handler;
}
public SomeClass()
{
m_handler = DefaultHandler.For<T>() ?? FALL_BACK_HANDLER;
}
public void DoSomeStuff(T input)
{
T result = m_handler(input);
Console.WriteLine(result);
}
}
public static class DefaultHandler
{
public static Func<T,T> For<T>()
{
return TypeAware<T>.Default;
}
private static class TypeAware<T>
{
private static readonly Func<T,T> DEFAULT;
static TypeAware()
{
var type = typeof(T);
if (type == typeof(string))
{
DEFAULT = a => DefaultHandler.StringHandler((dynamic) a);
}
else if (type == typeof(int))
{
DEFAULT = a => DefaultHandler.IntHandler((dynamic) a);
}
else
{
DEFAULT = null;
}
}
public static Func<T,T> Default { get { return DEFAULT; } }
}
public static string StringHandler(string a)
{
return a + " The default handler does some stuff!";
}
public static int IntHandler(int a)
{
return a + 2;
}
}
}
You would then consume SomeClass as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Zoltan
{
public class Program
{
public static void Main(string[] args)
{
var someStringObj = new SomeClass<string>();
someStringObj.DoSomeStuff("Hello World.");//prints "Hello World. The default handler does some stuff!"
var someIntObj = new SomeClass<int>();
someIntObj.DoSomeStuff(1);//prints 3
var someCustomDoubleObj = new SomeClass<double>(d => d - 2);
someCustomDoubleObj.DoSomeStuff(3);//prints 1
Console.Read();
}
}
}
Building on Jon Skeet and Alexei Levenkovs comments. From what I understand, something like this might be what you're after?
public delegate T SomeHandler<T>(T input);
public class SomeClass<T>
{
private SomeHandler<T> m_handler;
public SomeClass()
{
m_handler = (T input) => input;
}
public SomeClass(SomeHandler<T> handler)
{
m_handler = handler;
}
public void DoSomeStuff(T input)
{
T result = m_handler(input);
// some stuff
}
}
Another way would be to move the string-specific behaviour into a separate class and simply make an instance of that class if you want specific behaviour tied to a specific type
public delegate T SomeHandler<T>(T input);
public class SomeClass<T>
{
protected SomeHandler<T> m_handler;
protected SomeClass()
{
}
public SomeClass(SomeHandler<T> handler)
{
m_handler = handler;
}
public void DoSomeStuff(T input)
{
T result = m_handler(input);
// some stuff
}
}
public class SomeStringClass : SomeClass<string>
{
public SomeStringClass()
{
m_handler = DefaultStringHandler;
}
private string DefaultStringHandler(string input)
{
// Do default string stuff here...
return input;
}
public SomeStringClass(SomeHandler<string> handler):base(handler)
{
}
}

Serialize a method invocation to JSON then deserialize and call the method

I need to find a way to serialize a method call and it's associated parameter. This is because we'll be passing payloads onto a queue (like MSMQ) and then later on a process will pick up the message and need to deserialize the payload and call the method required.
Both sides of the queue use the same C# library, but one is inside a Web context and the other is inside a batch process / console app.
I have demonstrated below what I'd like to do, however, I realise it may not be possible. I know I could always encapsulate the meta data of what target method call and the have some huge switch statement that maps a parameter/payload to a method, however, it would be very cool and succinct if I could call any method I want (it doesn't matter whether it's static).
namespace SerializableMethodCalls
{
public class DTO
{
public string MyData { get; set; }
public int AnInteger { get; set; }
}
public class DTO2
{
public string MyData2 { get; set; }
public int AnInteger2 { get; set; }
}
class Program
{
private static Queue<string> _queue = new Queue<string>();
static void Main(string[] args)
{
DTO payload = new DTO
{
AnInteger = 45678,
MyData = "Test"
};
DTO2 payload2 = new DTO2
{
AnInteger2 = 534653,
MyData2 = "test2"
};
DoSomething(payload);
DoSomething(payload2);
_queue.Enqueue(Serialize(DoSomething, payload));
while (_queue.Count > 0)
{
var message = _queue.Dequeue();
DeserializeAndCallMethod(message);
}
}
private static void DeserializeAndCallMethod(string message)
{
// somehow deserialize the method invocation and then call the method with the serialized payload!
}
public static void DoSomething(DTO2 payload2)
{
Console.WriteLine("Done2! {0}, {1}", payload2.AnInteger2, payload2.MyData2);
}
public static void DoSomething(DTO dto)
{
Console.WriteLine("Done! {0}, {1}", dto.AnInteger, dto.MyData);
}
public static string Serialize(Method method, object parameter)
{
// somehow serialize a method call!?
}
}
}
I'd be interested in hearing your thoughts
Many thanks
Kris
Convert with the demonstrated From/To... methods between your DTOs and reflective objects:
class TypeDTO {
public string AssemblyName;
public string ClassName;
public static TypeDTO FromType(Type type) {
return new TypeDTO() {
AssemblyName = type.Assembly.FullName,
ClassName = type.FullName
};
}
public Type ToType() {
return ToType(AppDomain.CurrentDomain);
}
public Type ToType(AppDomain domain) {
Assembly assembly = domain.GetAssemblies().Where(t => t.FullName == AssemblyName).Single();
return assembly.GetType(ClassName);
}
}
class MethodSignatureDTO {
public TypeDTO DeclaringType;
public string MethodName;
public TypeDTO[] ParameterTypes;
public static MethodSignatureDTO FromMethod(MethodInfo method) {
return new MethodSignatureDTO() {
DeclaringType = TypeDTO.FromType(method.DeclaringType),
MethodName = method.Name,
ParameterTypes = method.GetParameters().Select(t => TypeDTO.FromType(t.ParameterType)).ToArray()
};
}
public MethodInfo ToMethod() {
return ToMethod(AppDomain.CurrentDomain);
}
public MethodInfo ToMethod(AppDomain domain) {
Type[] parameterTypes = ParameterTypes.Select(t => t.ToType(domain)).ToArray();
return DeclaringType.ToType(domain).GetMethod(MethodName, parameterTypes);
}
}
class MethodCallDTO {
public MethodSignatureDTO Method;
public object Instance;
public object[] Arguments;
public object Invoke() {
return Invoke(AppDomain.CurrentDomain);
}
public object Invoke(AppDomain domain) {
return Method.ToMethod(domain).Invoke(Instance, Arguments);
}
}

Getting property values from a static class using reflection

I have a class that is used for storing user data to a file. It works well, but can't really be placed into a PCL library easily. Outside of the PCL, it's all fine.
The class looks like this
public static class UserData
{
public static object GetPropertyValue(this object data, string propertyName)
{
return data.GetType().GetProperties().Single(pi => pi.Name == propertyName).GetValue(data, null);
}
public static object SetPropertyValue<T>(this object data, string propertyName, T value)
{
data.GetType().GetProperties().Single(pi => pi.Name == propertyName).SetValue(data, value);
return new object();
}
private static string pUserSettingsFile;
private static UserSettings userSetting;
public static bool AccountEnabled
{
get
{
return UserSettings.account_enabled;
}
set
{
UserSettings settings = UserSettings;
settings.account_enabled = value;
UserSettings = settings;
}
}
public static UserSettings UserSettings
{
get
{
if (userSetting == null)
{
if (File.Exists(UserSettingsFile))
{
userSetting = Serializer.XmlDeserializeObject<UserSettings>(UserSettingsFile);
}
else
{
userSetting = new UserSettings();
Serializer.XmlSerializeObject(userSetting, UserSettingsFile);
}
}
return userSetting;
}
set
{
if (value == null)
{
throw new ArgumentNullException("value is null!");
}
userSetting = value;
if (File.Exists(UserSettingsFile))
{
File.Delete(UserSettingsFile);
}
Serializer.XmlSerializeObject(userSetting, UserSettingsFile);
}
}
public static string UserSettingsFile
{
get
{
if (string.IsNullOrEmpty(pUserSettingsFile))
{
pUserSettingsFile = Path.Combine(GroupShootDroid.Singleton.ContentDirectory, "UserSettings.xml");
}
return pUserSettingsFile;
}
}
#endregion
}
public class UserSettings
{
public bool account_enabled { get; set; }
public string address { get; set; }
public string country { get; set; }
}
It's not rocket science, but does what I need it to do.
What I'm trying to do is use the Get/SetPropertyValue methods to return or set any of the properties within the class.
Currently, to access the Get/SetPropertyValue methods I'm using this
public string GetStringValue(string valToGet)
{
string rv = (string)UserData.GetPropertyValue(valToGet);
return rv;
}
public void SetStringValue(string name, string val)
{
UserData.SetPropertyValue(name, val);
}
On compiling though, the GetPropertyValue method is giving an error that No overload for method GetPropertyValue takes 1 argument with the SetPropertyValue complaining that there isn't an overload that takes 2
I'm not sure that the code I'm using will do what I need it to do (from what I've read on here it should be), but I'm more perplexed as to why the errors are showing.
Is there a better way to do what I'm trying to do? The application is a Xam.Forms app, so the PCL accesses the class through an interface using injection.
You are defining extension method, you need an instance of the class to call them:
var o = new Object();
string rv = (string)o.GetPropertyValue(valToGet);
// or, but no sure
string rv = (string)UserData.GetPropertyValue(o, valToGet);
or more probably in your case:
public string GetStringValue(string valToGet)
{
string rv = (string)this.GetPropertyValue(this, valToGet);
//or
//string rv = (string)UserData.GetPropertyValue(this, valToGet);
return rv;
}
I think you're getting confused between the UserData class and the object class. Your extension methods extend object.

Modify existing WCF communication object

This is how I used to make method calls:
SvcHelper.Using<SomeWebServiceClient>(proxy =>
{
proxy.SomeMethod();
}
public class SvcHelper
{
public static void Using<TClient>(Action<TClient> action) where TClient : ICommunicationObject, IDisposable, new()
{
}
}
This is how I make method calls:
ChannelFactory<ISomethingWebService> cnFactory = new ChannelFactory<ISomethingWebService>("SomethingWebService");
ISomethingWebService client = cnFactory.CreateChannel();
using (new OperationContextScope((IContextChannel)client))
{
client.SomeMethod();
}
My question is: Instead of replacing every instance of my original method call approach; Is there a way to modify my SvcHelper and do the creation of the channel in the SvcHelper constructor and then simply pass the interface like the following:
SvcHelper.Using<ISomethingWebService>(client =>
{
client.SomeMethod();
}
Hope this makes sense and thanks in advance.
First, you don't want to create a new ChannelFactory<T> every call to the Using helper method. They are the most costly thing to construct in the WCF universe. So, at bare minimum, you will want to use a caching approach there.
Second, you don't want to tie yourself to "client" types at all anymore. Just work straight with the service contract interfaces.
Starting from what you've got, here's where I'd go based on how I've done this in the past:
public class SvcHelper
{
private static ConcurrentDictionary<ChannelFactoryCacheKey, ChannelFactory> ChannelFactories = new ConcurrentDictionary<ChannelFactoryCacheKey, ChannelFactory>();
public static void Using<TServiceContract>(Action<TServiceContract> action) where TServiceContract : class
{
SvcHelper.Using<TServiceContract>(action, "*");
}
public static void Using<TServiceContract>(Action<TServiceContract> action, string endpointConfigurationName) where TServiceContract : class
{
ChannelFactoryCacheKey cacheKey = new ChannelFactoryCacheKey(typeof(TServiceContract), endpointConfigurationName);
ChannelFactory<TServiceContract> channelFactory = (ChannelFactory<TServiceContract>)SvcHelper.ChannelFactories.GetOrAdd(
cacheKey,
missingCacheKey => new ChannelFactory<TServiceContract>(missingCacheKey.EndpointConfigurationName));
TServiceContract typedChannel = channelFactory.CreateChannel();
IClientChannel clientChannel = (IClientChannel)typedChannel;
try
{
using(new OperationContextScope((IContextChannel)typedChannel))
{
action(typedChannel);
}
}
finally
{
try
{
clientChannel.Close();
}
catch
{
clientChannel.Abort();
}
}
}
private sealed class ChannelFactoryCacheKey : IEquatable<ChannelFactoryCacheKey>
{
public ChannelFactoryCacheKey(Type channelType, string endpointConfigurationName)
{
this.channelType = channelType;
this.endpointConfigurationName = endpointConfigurationName;
}
private Type channelType;
public Type ChannelType
{
get
{
return this.channelType;
}
}
private string endpointConfigurationName;
public string EndpointConfigurationName
{
get
{
return this.endpointConfigurationName;
}
}
public bool Equals(ChannelFactoryCacheKey compareTo)
{
return object.ReferenceEquals(this, compareTo)
||
(compareTo != null
&&
this.channelType == compareTo.channelType
&&
this.endpointConfigurationName == compareTo.endpointConfigurationName);
}
public override bool Equals(object compareTo)
{
return this.Equals(compareTo as ChannelFactoryCacheKey);
}
public override int GetHashCode()
{
return this.channelType.GetHashCode() ^ this.endpointConfigurationName.GetHashCode();
}
}
}
This should work:
public class SvcHelper
{
public static void Using<TClient>(Action<TClient> action) where TClient : ICommunicationObject, IDisposable
{
ChannelFactory<TClient> cnFactory = new ChannelFactory<TClient>("SomethingWebService");
TClient client = cnFactory.CreateChannel();
using (new OperationContextScope((IContextChannel)client))
{
action(client);
}
}
}

Categories