Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用#if和#define指定访问器_C# - Fatal编程技术网

C# 使用#if和#define指定访问器

C# 使用#if和#define指定访问器,c#,C#,对于C#中的跨平台库,我希望有一组特定的方法被标记为protected,以便于扩展。这些方法稍后通过反射访问,使用带有属性的元编程 然而,在WindowsPhone7上,不允许通过反射访问受保护的方法,相反,我希望它们被标记为内部 所以我想知道的是,我是否可以在C#中做类似的事情,或者是否有更好的解决方法 using System; using System.Collections.Generic; using System.Linq; using System.Text; #if WINDO

对于C#中的跨平台库,我希望有一组特定的方法被标记为protected,以便于扩展。这些方法稍后通过反射访问,使用带有属性的元编程

然而,在WindowsPhone7上,不允许通过反射访问受保护的方法,相反,我希望它们被标记为内部

所以我想知道的是,我是否可以在C#中做类似的事情,或者是否有更好的解决方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

#if WINDOWS_PHONE
    #define ACCESSOR internal
#else
    #define ACCESSOR protected
#endif

namespace Example
{
    public class MyTestClass
    {
        [MyAttribute]    
        ACCESSOR void MyMethod()
        {
        }
    }
}
您可以这样做:

[MyAttribute]
#if WINDOWS_PHONE 
internal
#else
protected
#endif
void MyMethod()
{         
} 

但是你最好让它们
内部
受保护的内部

你可以这样做:

[MyAttribute]
#if WINDOWS_PHONE 
internal
#else
protected
#endif
void MyMethod()
{         
} 

但是你最好让它们成为
内部的
受保护的内部的

我认为你不能用常量来代替语言结构,你应该能做的是:

namespace Example
{
    public class MyTestClass
    {
        [MyAttribute]
    #if WINDOWS_PHONE
        internal void MyMethod()
    #else
        protected void MyMethod()
    #endif
        {
        }
    }
}

我认为你不能用常量来代替语言结构,你应该能做的是:

namespace Example
{
    public class MyTestClass
    {
        [MyAttribute]
    #if WINDOWS_PHONE
        internal void MyMethod()
    #else
        protected void MyMethod()
    #endif
        {
        }
    }
}

我相信这会奏效:

namespace Example
{
    public class MyTestClass
    {
        [MyAttribute]    
#if WINDOWS_PHONE
        internal void MyMethod()
#else
        protected void MyMethod()
#endif
        {
        }
    }
}

我相信这会奏效:

namespace Example
{
    public class MyTestClass
    {
        [MyAttribute]    
#if WINDOWS_PHONE
        internal void MyMethod()
#else
        protected void MyMethod()
#endif
        {
        }
    }
}
不能以这种方式使用
#定义
。根据MSDN,它不像C

#define
用于定义符号。使用符号作为传递给#if指令的表达式时,表达式的计算结果将为true

罗伯特·皮特的答案看起来是一个很好的解决办法。

你不能用这种方式定义。根据MSDN,它不像C

#define
用于定义符号。使用符号作为传递给#if指令的表达式时,表达式的计算结果将为true


罗伯特·皮特的答案看起来是一个很好的解决办法。

如果成员被标记为受保护,我会得到一个安全例外。所以,除非“内部保护”并不意味着它是受保护的,否则我不确定它会有什么不同。同样,将它们标记为内部将意味着没有来自其他程序集的可扩展性(这与整个想法背道而驰)
protectedinternal
意味着它是受保护的或内部的。试一试。如果成员被标记为受保护,我会得到一个安全异常。所以,除非“内部保护”并不意味着它是受保护的,否则我不确定它会有什么不同。同样,将它们标记为内部将意味着没有来自其他程序集的可扩展性(这与整个想法背道而驰)
protectedinternal
意味着它是受保护的或内部的。试试看。