小编Sil*_*ind的帖子

将T转换为bool,反之亦然

我有以下扩展方法,使字符串能够做到这一点("true").As<bool>(false) 特别是对于布尔值,它将用于AsBool()做一些自定义转换.不知怎的,我不能从T转向Bool,反之亦然.我使用下面的代码工作,但它似乎有点矫枉过正.

这是关于这一行:
(T)Convert.ChangeType(AsBool(value, Convert.ToBoolean(fallbackValue)), typeof(T))
我宁愿使用以下内容,但它不会编译:
(T)AsBool(value, (bool)fallbackValue), typeof(T))

我错过了什么或者这是最短的路要走?

    public static T As<T>(this string value)
    {
        return As<T>(value, default(T));
    }
    public static T As<T>(this string value, T fallbackValue)
    {
        if (typeof(T) == typeof(bool))
        {
            return (T)Convert.ChangeType(AsBool(value,
                                                Convert.ToBoolean(fallbackValue)),
                                                typeof(T));
        }
        T result = default(T);
        if (String.IsNullOrEmpty(value))
            return fallbackValue;
        try
        {
            var underlyingType = Nullable.GetUnderlyingType(typeof(T));
            if (underlyingType == null)
                result = (T)Convert.ChangeType(value, typeof(T));
            else if (underlyingType == typeof(bool))
                result = (T)Convert.ChangeType(AsBool(value,
                                                Convert.ToBoolean(fallbackValue)),
                                                typeof(T));
            else
                result = (T)Convert.ChangeType(value, …
Run Code Online (Sandbox Code Playgroud)

c# casting type-conversion

7
推荐指数
1
解决办法
4784
查看次数

Html帮助程序不使用自定义VirtualPathProvider

我已经设置了一个VirtualPathProvider,它可以正常工作,如http://.../home/index地址栏中的直接网址调用.

public class HomeController
{   
    public ActionResult Index()
    {
        // This triggers MyVirtualPathProvider functionallity when called via
        // the browsers address bar or anchor tag click for that matter.
        // But it does not trigger MyVirtualPathProvider for 'in-view' calls like
        // @{ Html.RenderAction("index", "home"); }
        return View();
    }
}

public class MyVirtualPathProvider : VirtualPathProvider
{

    public override System.Web.Hosting.VirtualFile GetFile(string virtualPath)
    {
        // This method gets hit after the Controller call for return View(...);
        if (MyCondition)
            return …
Run Code Online (Sandbox Code Playgroud)

c# razor asp.net-mvc-4

7
推荐指数
1
解决办法
263
查看次数

添加导航:Frame导致NullReferenceException

我正在使用Silverlight 5构建一个3D应用程序.我有一个调用方法的DrawingSurface.但是,当我向我的XAML添加导航:Frame时,我会抛出一个错误.

这是有问题的方法:

private void DrawingSurface_Draw(object sender, DrawEventArgs e)
{

    GraphicsDevice device = GraphicsDeviceManager.Current.GraphicsDevice;

    device.Clear(ClearOptions.Target | ClearOptions.DepthBuffer,
                 new Microsoft.Xna.Framework.Color(0, 0, 0, 0), 10.0f, 0);

    device.SetVertexBuffer(_vertexBuffer);
    device.SetVertexShader(_vertexShader);
    device.SetPixelShader(_pixelShader);
    device.Textures[0] = _texture;

    device.SamplerStates[0] = SamplerState.LinearClamp;
    device.DrawPrimitives(PrimitiveType.TriangleList, 0,
                          _vertexBuffer.VertexCount / 3);
    device.SetVertexShaderConstantFloat4(0, ref _viewProjection);

    e.InvalidateSurface();

}
Run Code Online (Sandbox Code Playgroud)

错误在线device.DrawPrimitives(PrimitiveType.TriangleList, 0, _vertexBuffer.VertexCount / 3);.错误是"NullReferenceException未被用户代码处理".没有导航就不会发生:框架.

c# silverlight xna visual-studio-2010 silverlight-5.0

7
推荐指数
1
解决办法
240
查看次数

Linq 在加入过程后更新不同的表

我使用 linq join 组合了我的 3 个表。之后,我想使用从 webform 获取的数据更新此表。我怎样才能做到这一点 ?我的实现如下

public void updateShoes(Shoe shoe) 
{
    var query = from b in db.BrandTbls.AsQueryable()
                join m in db.ShoeModelTbls on b.BrandID equals m.BrandID
                join s in db.ShoeTbls on m.ModelID equals s.ModelID
                where shoe.ShoeID == s.ShoeID 
                orderby m.ModelName
                select new 
                { 
                    s.ShoeID,
                    s.Size,
                    s.PrimaryColor,
                    s.SecondaryColor,
                    s.Quantity,
                    m.ModelName,
                    m.Price,
                    b.BrandName
                };
}
Run Code Online (Sandbox Code Playgroud)

c# sql linq asp.net join

2
推荐指数
1
解决办法
8487
查看次数

Unity 错误 CS0236:字段初始值设定项无法引用非静态字段、方法或属性“UnitySampleAssets._2D.Camera2DFollow.Characters”

我意识到有数百页与此错误相关。然而,我已经查看了其中的许多内容,但无法找到与我的特定问题相关的内容,因此,如果这是重复的线程,我深表歉意。我正在尝试修改 Unity 提供的 2D 相机脚本,以将其“目标”(它遵循的游戏对象)选择为用户选择的任何角色。我是 C# 新手,所以我不太清楚如何处理这个错误。我发布了整个代码以防万一,但错误应该可以在前 10 行左右找到

using UnityEngine;

namespace UnitySampleAssets._2D
{

    public class Camera2DFollow : MonoBehaviour
    {
        private string character = PlayerPrefs.GetString("Character")
        public Transform target = Transform.Find(character);
        public float damping = 1;
        public float lookAheadFactor = 3;
        public float lookAheadReturnSpeed = 0.5f;
        public float lookAheadMoveThreshold = 0.1f;

        private float offsetZ;
        private Vector3 lastTargetPosition;
        private Vector3 currentVelocity;
        private Vector3 lookAheadPos;

        // Use this for initialization
        private void Start()
        {
            lastTargetPosition = target.position;
            offsetZ = (transform.position - target.position).z;
            transform.parent …
Run Code Online (Sandbox Code Playgroud)

c#

-1
推荐指数
1
解决办法
6700
查看次数